Modeladmin helpers

A collection of variables and objects for helping integrate the various models into a Django AdminSite.

Re-usable callable class origins

The various callable classes were inspired by work cdunklau in the #django IRC channel showed me, as demonstrated by code he released into the public domain in a paste, where previously they would have been Mixin objects.

class helpfulfields.admin.LogEntrySparkline(days=14, label=u'change history')[source]

An object capable of being used in the ModelAdmin list_display to show a tiny HTML-only sparkline of recent changes made via the admin:

class MyModelAdmin(ModelAdmin):
    list_display = ['pk', LogEntrySparkline(days=60)]

An example of the output is provided below, though it may render slightly differently due to font-sizing differences between this documentation and the standard Django AdminSite:

class helpfulfields.admin.RelationCount(accessor, label)[source]

An object capable of being used in the ModelAdmin list_display to enable a count of related items:

class MyModelAdmin(ModelAdmin):
    list_display = ['pk', RelationCount('relation_name', 'item count')]

which adds a new column to the admin which shows the results of obj.accessor.count() and the verbose name.

Note

We expect to be able to address the relation from the obj instance. As such, reverse relations denied via setting a related_name of + won’t work.

Warning

This should result in a maximum of one additional query being executed, per object, per usage, to get a count of related objects.

class helpfulfields.admin.RelationList(accessor, label, max_num=3, more_separator=None, admin_site='admin')[source]

An object capable of being used in the ModelAdmin list_display to show a linked list of related items:

class MyModelAdmin(ModelAdmin):
    list_display = ['pk', RelationList('accessor', 'item count')]

which adds a new column to the admin which shows the results of obj.accessor.all() as links to the appropriate modeladmin page.

Note

We expect to be able to address the relation from the obj instance. As such, reverse relations denied via setting a related_name of + won’t work.

class helpfulfields.admin.ViewOnSite(text=u'view on site', label=u'view on site')[source]

An object capable of being used in the ModelAdmin list_display to enable a link to the current object on the frontend of the website:

class MyModelAdmin(ModelAdmin):
    list_display = ['pk', ViewOnSite('column name', 'view on site!')]

which shows a link to view an object on the live site, assuming the obj has get_absolute_url() defined.

helpfulfields.admin.changetracking_fieldset = [u'changes', {'fields': ['created', 'modified'], 'classes': ['collapse']}]

a fieldset for use in a ModelAdmin fieldsets definition to display objects which are making use of the created and modified fields provided by ChangeTracking. The fieldset provides a translated name via changetracking_fieldset_label, and starts out collapsed as the data is unimportant.

helpfulfields.admin.changetracking_readonlys = ['created', 'modified']

a list for use in a ModelAdmin‘s readonly_fields configuration to avoid allowing editing of the created and modified fields provided by ChangeTracking

helpfulfields.admin.date_publishing_fieldset = [u'publishing info', {'fields': ['publish_on', 'unpublish_on'], 'classes': []}]

a fieldset for use in a ModelAdmin fieldsets definition to display objects which are making use of the publish_on and unpublish_on provided by DatePublishing. The fieldset provides a translated name via dates_fieldset_label

helpfulfields.admin.publishing_fieldset = [None, {'fields': ['is_published'], 'classes': []}]

a fieldset for use in a ModelAdmin fieldsets definition to display objects which are making use of the is_published field provided by Publishing. This fieldset does not provide a name, because it doesn’t make much sense for one field.

helpfulfields.admin.seo_fieldset = [u'search engine optimisation', {'fields': ['meta_title', 'meta_description', 'meta_keywords'], 'classes': ['collapse']}]

a fieldset for use in a ModelAdmin fieldsets definition to display objects which are making use of the meta_title, meta_description and meta_keywords provided by SEO. The fieldset provides a translated name via seo_fieldset_label, and collapses itself by default.

helpfulfields.admin.titles_fieldset = [None, {'fields': ['title', 'menu_title'], 'classes': []}]

a fieldset for use in a ModelAdmin fieldsets definition to display objects which are making use of the title and menu_title provided by Titles. This fieldset does not provide a name, as the field names should be self descriptive at a very basic level.

Project Versions

Previous topic

Mixing model querysets

Next topic

Re-usable Localising strings

This Page