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
Using GFK Resolved Values
You might want to utilize the object of the GenericForeignKey field somewhere
in your form code, such as your clean method. To do this use the
get_generic_field_name_for method.
class GenreAdminForm(GenericFKModelForm):
class Meta:
model = Genre
fields = "__all__"
def clean(self):
super().clean()
media_generic_field = self.get_generic_field_name_for("media")
media = self.cleaned_data[media_generic_field]
if media.name.lower() != media.name:
raise ValidationError({media_generic_field: "media name must be lowercase"})
return self.cleaned_data
A complete example django app exists in this repository at here
Release files for django-genfkadmin 0.3.2
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.2.tar.gz | 13.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_genfkadmin-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.3 kB
Release files / django_genfkadmin-0.3.2.tar.gz
| Download URL | django_genfkadmin-0.3.2.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7067593b389bc0049201a1c41fce2b5a2f57557b5b331c103464ba9e7487cc2
|
|
BLAKE2b-256 checksum How to use checksums |
26c27848fb2e3d7bc47af34b23e632a6fa2a6df214730736eaae0e7f4047ce70
|
| 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.2-py3-none-any.whl
| Download URL | django_genfkadmin-0.3.2-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
855ef6ba6d34e76b591688e77a80b8fd7fee07732108679171a7251c414b4767
|
|
BLAKE2b-256 checksum How to use checksums |
f37ded770cc86eec35eee886b07da354a055d465100b2aaa50a4d8a9922b3ee6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|