An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# django-admin-backref

[![PyPi version](https://badgen.net/pypi/v/django-admin-backref/)](https://pypi.python.org/pypi/django-admin-backref/)
[![Documentation Status](https://readthedocs.org/projects/django-admin-backref/badge/?version=latest)](http://django-admin-backref.readthedocs.io/?badge=latest)
[![PyPi license](https://badgen.net/pypi/license/django-admin-backref/)](https://pypi.python.org/pypi/django-admin-backref/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](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.