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 define a
method on your admin class 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.1
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.1.tar.gz | 12.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_genfkadmin-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.9 kB
Release files / django_genfkadmin-0.3.1.tar.gz
| Download URL | django_genfkadmin-0.3.1.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7915555dd3ed2c0a6ca3b6a36157983223a705989671efa90cfec3c646899cfd
|
|
BLAKE2b-256 checksum How to use checksums |
a6cb895dbc8984b0ec1243cda9cc9c2e79e5a171aa2b4e25bcbde14dc57ca6e5
|
| 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.1-py3-none-any.whl
| Download URL | django_genfkadmin-0.3.1-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9996f8241a8b539cd7d4360963ceb342140a705e98cdba9a28c41ad5e43f2610
|
|
BLAKE2b-256 checksum How to use checksums |
42c6dbf3d8404e07f2d374beceb5257083375c21f814a3b9aa479b946b2ced45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|