Skip to main content

Link related objects in Django admin using decorators

Project description

Django Admin Anchors

PyPI PyPI - Python Version PyPI - Django Version Codecov License

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

Object list page (light mode) Object list page (dark mode)

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

Object change page (light mode) Object change page (dark mode)

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:

  1. poetry install
  2. poetry run python tests/project/manage.py migrate
  3. poetry run python tests/project/manage.py createsuperuser
  4. poetry run python tests/project/manage.py runserver

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_anchors-4.3.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

django_admin_anchors-4.3.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_anchors-4.3.0.tar.gz.

File metadata

  • Download URL: django_admin_anchors-4.3.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.2 Linux/6.13.8-arch1-1

File hashes

Hashes for django_admin_anchors-4.3.0.tar.gz
Algorithm Hash digest
SHA256 acd840b90b50be00df9b51fd0468ffb4f5ea6f696393b31978638918e309cf60
MD5 25e5c056673d650bee989c31251676b7
BLAKE2b-256 41c2b622dceb9af3ef49a33f3dd00827c1333933db55eb7297c4e8ce5f41c07a

See more details on using hashes here.

File details

Details for the file django_admin_anchors-4.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_anchors-4.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 109faa47b6d4f509f6f25f77a93e53ca2c4a09a476da6486d186226597287db9
MD5 63d951441498a9e87a1c9b3c7cfbf531
BLAKE2b-256 6bc67ddac062b2b2ff6cbb6721f371743f6ec307a05ebd10e994ff18766ed89b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page