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.
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 edit 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"
Contributing
Setup
- Clone the repository and enter the cloned folder
- (optional) Create and activate a dedicated Python virtual environment
- Run
pip install -e ".[dev]"
to install the projects requirements - (optional) Run
pre-commit install
to install the pre-commit hook
Pre-commit hook
Our pre-commit hook formats and lints the code.
Formatting and linting
- Run
black admin_anchors tests
to format the code - Run
flake8 admin_anchors tests
to lint the code
Testing
- Run
py.test --cov admin_anchors tests
to run the tests in the current Python env - Run
tox
to run the tests in all supported Python and Django environments
Makefile
All commands listed above have shortcut make recipes.
Take a look at the Makefile
to learn more.
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-2.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d0526787918a26f6bace78f8d6993832358f222c316f98747dcbe846ba9d69c |
|
MD5 | be5cd2b6cadcd897d9715a78c8e0a001 |
|
BLAKE2b-256 | b1d7e81e1b6328d406af02cdc28afe84590927f8cdf8ac239d52a8757e66056d |
Close
Hashes for django_admin_anchors-2.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d9676b0cabb74b7312043096ce546923bee7fbe1c67d0110fd890dccfe0e572 |
|
MD5 | a6e1395b1216744741fda203ed3b5138 |
|
BLAKE2b-256 | 5a0a6594adf1c31d17844e7505398e54664aa043f522c6b5a52efaac08c6261a |