No frills GenericForeignKeys for your Django Admin
Project description
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
from genfkadmin.forms import GenericFKModelForm
class PetAdminForm(GenericFKModelForm):
class Meta:
model = Pet
fields = "__all__"
@admin.register(Pet)
class PetAdmin(GenericFKAdmin):
form = PetAdminForm
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(Pet)
class PetAdmin(GenericFKAdmin):
form = PetAdminForm
def get_form(self, request, obj=None, change=False, **kwargs):
if obj:
self.form = partial(
PetAdminForm,
filter_callback=lambda queryset: queryset.filter(
tags__owner=obj.owner
),
)
return super().get_form(request, obj=obj, change=change, **kwargs)
A complete example django app exists in this repository at here
Roadmap
- Stacked and Tabular Inline support.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_genfkadmin-0.1.1.tar.gz.
File metadata
- Download URL: django_genfkadmin-0.1.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd6cb0eb10c5d382a5165a7bb7cb098a507d880babce96c50269cece586533d2
|
|
| MD5 |
6053600f06203b865f08d805723cf5fa
|
|
| BLAKE2b-256 |
90775bfdf0b5572d399ce8a99e5fd59f7514678a56078f78b5e596eec0be611a
|
File details
Details for the file django_genfkadmin-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_genfkadmin-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0d5be4975135dc394109d0e3d0e988de0d11043062eea3afd6e7888234e20a1
|
|
| MD5 |
652bd1041baa1acf72845db3e2054db5
|
|
| BLAKE2b-256 |
dd3c12eb6e648b7f32a0fc4fbdbcfd3ed4f5232ef3337cc354e708a77c8aef3d
|