Turn Django admin list display items into clickable links to related objects using decorators.
Clicking admin anchors will redirect to a filtered changelist view showing the related objects. This allows you to get a quick overview and run actions on the filtered objects.
pip install django-admin-anchors
Take a look at the tests/project
directory to see a runnable example project.
from django.contrib import admin
from admin_anchors import admin_anchor
from yourapp.models import Team
@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
list_display = ["__str__", "captain_link", "captains_profile_link", "members_link"]
@admin_anchor("captain")
def captain_link(self, instance):
return str(instance.captain)
@admin_anchor("captain.profile")
def captains_profile_link(self, instance):
return "Captains profile"
@admin_anchor("members")
def members_link(self, instance):
return f"{instance.members.count()} members"
from django.contrib import admin
from admin_anchors import admin_anchor
from yourapp.models import Player
@admin.register(Player)
class PlayerAdmin(admin.ModelAdmin):
readonly_fields = ["profile_link"]
@admin_anchor("profile")
def profile_link(self, instance):
return "Profile"
Take a look at our Django example project under tests/project
.
You can run it by executing these commands:
poetry install
poetry run python tests/project/manage.py migrate
poetry run python tests/project/manage.py createsuperuser
poetry run python tests/project/manage.py runserver