https://github.com/hapytex/django-admin-backref
Generate admin URLs through the models.
https://github.com/hapytex/django-admin-backref
django django-admin urls
Last synced: about 2 months ago
JSON representation
Generate admin URLs through the models.
- Host: GitHub
- URL: https://github.com/hapytex/django-admin-backref
- Owner: hapytex
- Created: 2024-04-11T12:28:02.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-13T19:24:52.000Z (about 2 years ago)
- Last Synced: 2025-02-07T10:14:27.422Z (over 1 year ago)
- Topics: django, django-admin, urls
- Language: Python
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# django-admin-backref
[](https://pypi.python.org/pypi/django-admin-backref/)
[](http://django-admin-backref.readthedocs.io/?badge=latest)
[](https://pypi.python.org/pypi/django-admin-backref/)
[](https://github.com/psf/black)
Generate admin URLs through the models.
This package automatically attaches an `.admin_links` object to all models with a registered `ModelAdmin` (for the default admin site).
This can then be used to generate the links both from the perspective of the *model object* as well as the model. One can then generate the path, or a link to that path with:
```python3
User.admin_links # '/admin/auth/user/'
User.admin_links.add # '/admin/auth/user/add'
User.admin_links.label # '<class 'django.contrib.auth.models.User'>'
User(pk=1, username='username').admin_links # '/admin/auth/user/1/change/'
User(pk=1, username='username').admin_links.delete # '/admin/auth/user/1/delete/'
User(pk=1, username='username').admin_links.label # 'username'
User(pk=1, username='username').admin_links.add # '/admin/auth/user/add/'
```
## Installation
You install the package with:
```bash
pip install django-admin-backref
```
Next you can add the `django_admin_backref` to the `INSTALLED_APPS`:
```python3
# settings.py
INSTALLED_APPS = [
# …,
'django.contrib.admin',
# …,
'django_admin_backref',
# …
]
```
Next all the models that have been registered for the *default* admin site, will automatically have a `.admin_links` attribute to access the admin URLs. If a model has already such attribute, for example because some other package, it will *not* override this.
## Usage
The `.admin_links` will generate an `AdminLink` object. For each view name, it then has attributes. We can access these in four ways:
- .name: the URL for the admin link with that name;
- .name_label: a *safe* string that contains an `` to that admin URL, and uses the label of the object;
- .get_name(): the URL for the admin link with that name as a method; and
- .get_name_label(): a *safe* string that contains an `` to that admin URL, and uses the label of the object as a method.
For a simple model and for a simple admin, so without additional packages, for the class we have `.add`, `.changelist`, for *instances* it also contains `.change`, `.delete` and `.history`. Additional packages like `django-import-export` or special `ModelAdmin`s like the one for the `User` model introduce additional paths, like for example `.import` and `.password_change`.
The label is determined by first calling `.get_label()` on the model object, if that does not work it takes the `str(…)` of the instance or model class. Finally if these raise an error, it will take the empty string.