Package: django-admin-relation-links
Django Admin relation linksAn easy way to add links to relations in the Django Admin site.PreviewImagine you have admin pages for 2 models:MemberandGroup.Memberhas aForeignKeyrelation toGroup.When you look at a member, you want to easily navigate to its group.When you look at a group, you want to easily navigate to a list of the members in that group.With the help of this app you can easily create these links:Member list page:Member change page:Group list page:Group change page:Installpip install django-admin-relation-linksHow to useThe links are placed on thechange pageof the model and go to thechange
list pageor thechange pageof the related model, depending on whether the
related model has aForeignKeyto this model or this model has aForeignKeyto the related model, or if it's aOneToOneField.So for example, if you have these models:fromdjango.dbimportmodelsclassGroup(models.Model):name=models.CharField(max_length=200)classMember(models.Model):name=models.CharField(max_length=200)group=models.ForeignKey(Group,related_name='members')Then in the admin you can add links on theGroupchange pageto theMemberchange list page(all the members of that group) and on theMemberchange pagea link to theGroupchange page(the group of that member).
Use thechangelist_linksandchange_linksfields:fromdjango.contribimportadminfromdjango_admin_relation_linksimportAdminChangeLinksMixin@admin.register(Group)classGroupAdmin(AdminChangeLinksMixin,admin.ModelAdmin):list_display=['name']# Use the `related_name` of the `Member.group` field.# If you have no `related_name` specified on your model, use the default# `related_name` assigned by Django.changelist_links=['members']@admin.register(Member)classMemberAdmin(AdminChangeLinksMixin,admin.ModelAdmin):list_display=['name']# Here we just specify the name of the `ForeignKey` field.change_links=['group']List page linksIt is possible to show links on adminlist pageas well, using the field{field_name}_link:@admin.register(Member)classMemberAdmin(AdminChangeLinksMixin,admin.ModelAdmin):list_display=['name','group_link']# Show link to group *change page* on member *list page*change_links=['group']Link labelBy default, the label of the link is the string representation of the model
instance. You can change the label by creating a method named{field_name}_link_label()like this:defgroup_link_label(self,group):return'{}({}members)'.format(group.name,group.members.count())Extra optionsYou can also set extra options likelabel,modelandlookup_filterlike this:@admin.register(Group)classGroupAdmin(AdminChangeLinksMixin,admin.ModelAdmin):list_display=['name']changelist_links=[('members',{'label':'All members',# Used as label for the link'model':'Member',# Specify a different model, you can also specify an app using `app.Member`'lookup_filter':'user_group'# Specify the GET parameter used for filtering the queryset})]List page orderingWhen showing links on the list page, when you use that field for ordering, the
default ordering field is the first field in theorderingoption on theMetaclass of the model of the related field. You can specify an alternative
ordering like this:@admin.register(Group)classMemberAdmin(AdminChangeLinksMixin,admin.ModelAdmin):list_display=['name','group_link']change_links=[('group',{'admin_order_field':'group__name',# Allow to sort members by `group_link` column})]