Admin

Filters

class dj_kaos_utils.admin.BooleanAdminFilter(request, params, model, model_admin)

Bases: SimpleListFilter

An admin filter that works like an on-off switch; two options: on, off (all)

Example:
>>> class ByAvailableFilter(BooleanAdminFilter):
>>>     title = "availability"
>>>     parameter_name = 'is_available'
>>>
>>>     def filter(self, request, queryset):
>>>         return queryset.filter(is_available=True)
filter(request, queryset)

Override this method to filter the queryset when the filter value is set to True

Parameters
  • request – the request from the admin site

  • queryset – the queryset passed by the admin

Returns

filtered queryset

Mixins

class dj_kaos_utils.admin.EditReadonlyAdminMixin

Bases: BaseModelAdmin

Fields defined in edit_readonly_fields are editable upon creation, but after that they become readonly Set allow_superusers to True to allow superusers to edit such fields even in an edit form.

Example:
>>> class MyModelAdmin(EditReadonlyAdminMixin, admin.ModelAdmin):
>>>     edit_readonly_fields = ('slug',)

Inlines

class dj_kaos_utils.admin.NoViewInlineMixin(parent_model, admin_site)

Bases: InlineModelAdmin

Admin inline mixin that doesn’t show any objects (but can show the form to add).

class dj_kaos_utils.admin.NoAddInlineMixin(parent_model, admin_site)

Bases: InlineModelAdmin

Admin inline mixin that doesn’t show the form to add new objects

class dj_kaos_utils.admin.NoChangeInlineMixin(parent_model, admin_site)

Bases: InlineModelAdmin

Admin inline mixin that makes the existing objects not be editable.

Utils

dj_kaos_utils.admin.utils.pp_json(obj)

Return a syntax highlighted python dictionary/json in html

Parameters

obj – The dictionary/json to be pretty printed

Returns

syntax highlighted python dictionary/json in html

Example:
>>> class MyModelAdmin(admin.ModelAdmin):
>>>     ...
>>>     @admin.display
>>>     def json_display(self, obj):
>>>         return pp_json(obj.json)
dj_kaos_utils.admin.utils.render_element(tag, children=None, attrs=None)

Render safe html element

Parameters
  • tag – the html tag to render, i.e. p or h1

  • children – the children of the html element, will be escaped unless marked safe

  • attrs – dictionary of attributes to render on the element

Returns

safe html element with tag, attributes and children

Example:
>>> class MyModelAdmin(admin.ModelAdmin):
>>>     ...
>>>     @admin.display
>>>     def alert_display(self, obj):
>>>         return render_element('div', "Alert", {'class': 'alert'})
dj_kaos_utils.admin.utils.render_img(src: str, alt='', attrs=None)

Render img tag with src, alt and attrs

Parameters
  • src – src of img

  • alt – alt attribute

  • attrs – dict of extra attributes

Returns

safe html of img tag

Example:
>>> class MyModelAdmin(admin.ModelAdmin):
>>>     ...
>>>     @admin.display
>>>     def image_display(self, obj):
>>>         return render_img('image.jpg', "Image", {'class': 'thumbnail'})
dj_kaos_utils.admin.utils.render_anchor(href: str, children=None, attrs=None, new_tab=True, new_tab_icon=True)

Render anchor/link tag with href, children, etc.

Parameters
  • href – the href link of the anchor

  • children – what to render inside the anchor tag

  • attrs – other attributes to put on the element

  • new_tab – whether the link should open a new tab

  • new_tab_icon – whether to render a mini icon at the end of the link denoting it will open in a new tab. Only goes into effect if new_tab is True.

Returns

anchor tag

Example:
>>> class MyModelAdmin(admin.ModelAdmin):
>>>     ...
>>>     @admin.display
>>>     def url_display(self, obj):
>>>         return render_anchor('https://www.example.com', "Link")

Return the admin change page link for the given object, or None if the link cannot be generated.

Parameters

obj (django model instance) – the object for which to get the admin change page link

Returns

the admin change page link if it exists or None otherwise

Return type

str or None

Raises

NoReverseMatch if the admin change URL does not exist for the given object

Example:
>>> class MyModel(models.Model):
>>>     def get_absolute_url(self):
>>>         return get_admin_link(self)

Return marked-as-safe html containing an anchor tag linking to the admin change page of the object. If the object doesn’t have an admin change page, return the string representation of the object

Parameters
  • obj – the model instance to grab the link for

  • kwargs – extra kwargs to pass to render_anchor

Returns

marked-as-safe html of the anchor tag

Example:
>>> class MyModelAdmin(admin.ModelAdmin):
>>>     def parent_display(self, obj):
>>>         return render_admin_link(obj.parent)