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.
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:
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.
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.
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.
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.
a list for use in a ModelAdmin‘s readonly_fields configuration to avoid allowing editing of the created and modified fields provided by ChangeTracking
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
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.
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.
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.