Django GenericFKAdmin
Using GenericForeignKey in your Django models is cool, the default behavior of
Django Admin is not. This package allows you to replace the content_type and
object_id fields in your admin forms with a single input that is prefilled
with only the models related through GenericRelation fields.
Setup
Install
pip install django-genfkadmin
uv add django-genfkadmin
Usage
Using this package is pretty simple.
- Create a subclass of
GenericFKModelFormfor your model. - Create a subclass of
GenericFKAdminfor your model. - ???
- Profit!
e.g. in your admin.py
from genfkadmin.admin import GenericFKAdmin
@admin.register(Pet)
class PetAdmin(GenericFKAdmin):
pass
Providing a filter_callback
If you want to further filter the queryset (perhaps by something related to
the parent instance of your model with GenericForeignKey) you can pass a
partial with a keyword argument of filter_callback as follows.
@admin.register(MarketingMaterial)
class MarketingMaterialAdmin(GenericFKAdmin):
def filter_callback(
self,
queryset: QuerySet,
obj: MarketingMaterial | None = None,
):
if obj:
return queryset.filter(customer=obj.customer)
return queryset
Now when loading an existing MarketingMaterial, the content_object options are filtered by the chosen Customer
A complete example django app exists in this repository at here
Release files for django-genfkadmin 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_genfkadmin-0.3.0.tar.gz | 11.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_genfkadmin-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.7 kB
Release files / django_genfkadmin-0.3.0.tar.gz
| Download URL | django_genfkadmin-0.3.0.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d20b6c878d4cd356d8d7278130357e1653dc8855fa0a0faecdb77e26133e86b
|
|
BLAKE2b-256 checksum How to use checksums |
4845c619af6cf39f02bec28aa2013db36ef1f427bfa9d42d5f1d27387b8baf99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|
Release files / django_genfkadmin-0.3.0-py3-none-any.whl
| Download URL | django_genfkadmin-0.3.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fcdd2e695db380878564388c323b8cb3e9828431d7abffc4cbd9ff24fbedd39f
|
|
BLAKE2b-256 checksum How to use checksums |
daeb84431c0cc499aa964314710d73c3264f494ec3fb22db3e1e6477b0e03cb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|