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
File details
Details for the file django_admin_anchors-4.2.0.tar.gz
.
File metadata
- Download URL: django_admin_anchors-4.2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.6-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a5cd9abaff5ed347f33f0d84001b9c4252bfae55c72a4eb60009b0c7953a5b0 |
|
MD5 | 3f793762d30c4037d5f65f9d47093d77 |
|
BLAKE2b-256 | e230e26350cf3ff92953263f8ac232c2ec92b29d6b911303d308d38a5fe621e5 |
File details
Details for the file django_admin_anchors-4.2.0-py3-none-any.whl
.
File metadata
- Download URL: django_admin_anchors-4.2.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.6-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 486c542723d55beffa367a8f08b7c0c2acf9af6e9174ef8d69a096779bad3235 |
|
MD5 | 4b4eb1af1be06dca322c64ed705c208a |
|
BLAKE2b-256 | cdd01815f41404148217f1be9fcd116b821cdef806b2febf42dec490382fe0b8 |