Link related objects in Django admin using decorators
Project description
Django Admin Anchors
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.
Installation
pip install django-admin-anchors
Usage
Take a look at the tests/project
directory to see a runnable example project.
Add links to the object list page
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"
Add links to the object update page
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"
Example project
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_admin_anchors-4.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4ebaf07fff1305d753bad9f7f269a7279b8d717a4cdef9319036ecc409bdb88 |
|
MD5 | afda2bb36362d8aadacc6b6756c507d6 |
|
BLAKE2b-256 | b2b9341e0503094afe53aaefe744a2a2f6347540a0e72a5d7a78536687884676 |
Close
Hashes for django_admin_anchors-4.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a801e2ae44685ef68079a815c4c377ce4e316493c541662f7878a7d50069e24 |
|
MD5 | 8e0ceffac0baf896ade5471ea2fb6bed |
|
BLAKE2b-256 | 006fbd03b634b862f2e3c5630b86d3935fcf1be9269497d826cbb7bfc72cf2e4 |