django / django/new-features

Generic model function to get admin url

Open
#143 4 comments 4 reactions 0 assignees View on GitHub
Admin Models/ORM
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

I would like an generic way to retrieve the admin URL of a given object.

### Problem

Currently, each time I want to add the link to the admin of an object, I need to add on the model a custom function:

```
def get_admin_url(self) -> str:
return reverse(
f"admin:{self._meta.app_label}_{self._meta.model_name}_change",
args=[self.pk],
)
```

and each time, it is the same duplicate function.

### Request or proposal

proposal

### Additional Details

I known that having a model doesn't necessarily mean that there is an admin for this model. So adding a link between Model and Admin maybe not the correct solution.

### Implementation Suggestions

```
def get_admin_url(self):
if not admin.site.is_registered(self.__class__):
return None
return reverse(
f"admin:{self._meta.app_label}_{self._meta.model_name}_change", args=[self.pk]
)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.