If you add a lot of custom `ModelAdmin` methods to `list_display` like I do, you know it can require a lot of repetition. Notice how adding 'checkbox' to `list_display` requires typing the method name 4 times:
class ExampleAdmin(admin.ModelAdmin):
list_display = ['checkbox', '__str__']
def checkbox(self, object):
return '<input type="checkbox" value="%s"/>' % object.pk
checkbox.short_description = mark_safe('✓')
checkbox.allow_tags = True
Using this decorator, the name only needs to be typed once:
class ExampleAdmin(admin.ModelAdmin):
list_display = ['__str__']
@add(list_display, mark_safe('✓'), 0, allow_tags=True)
def checkbox(self, object):
return '<input type="checkbox" value="%s"/>' % object.pk
- admin
- modeladmin
- list_display
There are several nice ModelAdmin subclasses that provide useful functionality (such as django-batchadmin, django-reversion, and others), but unfortunately a ModelAdmin can really only subclass one at a time, making them mutually exclusive.
This snippet aims to make mixing these classes in as easy as possible -- you can inherit your model admin from it, add a tuple of mixins, and it will dynamically change the inheritance tree to match. This isn't guaranteed to work with all ModelAdmins, but so long as the mixins play nice with django modeladmin they *should* work.