Package: django-changelist-inline
Inline Changelists for DjangoOverviewDjango Admin comes withinlineswhich allows you to display forms to edit
related objects from the parent object’s change form. While it’s technically
possible to use this infrastructure to display list of related objects, it
gets hacky pretty fast.Here’s wheredjango-changelist-inlinecomes into play. It allows you to embed
the standard list of objects as an inline on the parent object’s change form.
Internally, it uses a subclass ofModelAdmin, which means you can customize
the list as you’d normally do. Additionally, you can display links below the
list of objects.Usage ExampleLet’s assume you have a Django project with two models -ThingandRelatedThingand that you want to display list ofRelatedThingobjects
as an inline inThingchange form. The follwing snippet provides an example
of how to implement that usingdjango-changelist-inline:# -*- coding: utf-8 -*-fromurllib.parseimporturlencodefromdjango.contribimportadminfromdjango.urlsimportreversefromdjango.utils.safestringimportmark_safefromdjango_changelist_inlineimport(ChangelistInline,ChangelistInlineAdmin,ChangelistInlineModelAdmin,)fromtesting.modelsimportRelatedThing,Thing@admin.register(RelatedThing)classRelatedThingModelAdmin(admin.ModelAdmin):list_display=('pk','name')classRelatedThingChangelistInline(ChangelistInline):model=RelatedThingclassChangelistModelAdmin(ChangelistInlineModelAdmin):list_display=('name','format_actions')list_display_links=Nonelist_per_page=5defget_queryset(self,request):returnRelatedThing.objects.filter(thing=self.parent_instance)@mark_safedefformat_actions(self,obj=None):ifobjisNone:returnself.empty_value_displaychange_link_url=reverse('admin:{app_label}_{model_name}_change'.format(app_label=RelatedThing._meta.app_label,model_name=RelatedThing._meta.model_name,),args=[obj.pk],)return(f'<a class="button" href="{change_link_url}">''Edit''</a>')format_actions.short_description='Actions'@propertydeftitle(self):return'Linked Related Things'@propertydefno_results_message(self):return'No Related Things?'defget_add_url(self,request):result=super().get_add_url(request)ifresultisnotNone:returnresult+'?'+urlencode({'thing':self.parent_instance.pk,})returnresultdefget_show_all_url(self,request):result=super().get_show_all_url(request)ifresultisnotNone:returnresult+'?'+urlencode({'thing':self.parent_instance.pk,})returnresultdefget_toolbar_links(self,request):return('<a href="https://www.bthlabs.pl/">''BTHLabs''</a>')@admin.register(Thing)classThingModelAdmin(ChangelistInlineAdmin):inlines=(RelatedThingChangelistInline,)APIChangelistInlineobjectsTheChangelistInlineclass is the center piece of the API. It’s
designed to be used in aModelAdmin’sinlines.In order for it to work, you’ll need to define themodelproperty and
embedChangelistModelAdminclass, which should be a subclass ofChangelistInlineModelAdmin.ChangelistInlineModelAdminobjectsTheChangelistInlineModelAdminis a customizedModelAdminsubclass
which provides sane defaults and additional functionality for inline
changelists.Changelist sanitizationThis subclass overrides the following attributes and methods ofModelAdminto provide sane defaults:list_editable- set to empty tuple to disable editing of the list,list_filter- set to empty tuple to disable filtering of the list,search_fields- set to empty tuple to disable searching,date_hierarchy- set toNone,sortable_by- set to empty tuple to disable sorting,get_actions()- returns empty list to disable actions.Additional functionalityTo allow customization and to expose additional functionality,ChangelistInlineModelAdminprovides the following additional methods:titleproperty - returns the model’sverbose nameby default.no_results_messageproperty - returns text to display in place of the
table if no objects are fetched from the DB.get_add_url(request)- returns URL for the model’s add form, if the
user has the add permission. ReturnNoneto hide the add link.get_show_all_url(request)- returns URL for the model’s changelist, if
the user has the view permission. ReturnNoneto hide the show all link.get_toolbar_links(request)- returnsNoneby default. Override this
to return string with additional<a/>elements to render in the toolbar.
The return value is marked safe in the template.ChangelistInlineAdminobjectsTheChangelistInlineAdminclass is a base class forModelAdminsubclasses that use inline changelists.ChangelistInlineAdminMixinA mixin class that is used to properly configure changelist inlines in the
parentModelAdmin. Overridesget_inlines(request, obj=None)andget_inline_instances(request, obj=None)methods.If you can’t useChangelistInlineAdminas you base class, you can use this
mixin to enable inline changelists:@admin.register(Thing)classThingModelAdmin(ChangelistInlineAdminMixin,MyBaseModelAdmin):...Authordjango-changelist-inlineis developed byTomek Wójcik.Licensedjango-changelist-inlineis licensed under the MIT License.