Custom Django admin mixin and widgets for handling generic foreign keys (GFKs) more intuitively.
Project description
admin_pretty_gfk
Custom Django admin mixin and widgets for handling generic foreign keys (GFKs) more intuitively.
Features
- PrettyGFKModelAdminMixin: A mixin that enhances Django's admin interface for models with GFKs.
- ForeignKeyContentIdWidget: A raw ID widget with a fallback URL to prevent errors.
- ContentTypeSelect: A dropdown widget that dynamically updates related object selection and resets the foreign key field when the content type changes.
Installation
pip install admin_pretty_gfk
Usage
Import and Use in ModelAdmin
Using the Mixin
from admin_pretty_gfk.mixins import PrettyGFKModelAdminMixin
from django.contrib import admin
class AdvertAdmin(PrettyGFKModelAdminMixin, admin.ModelAdmin):
pass
Using the Widget
from admin_pretty_gfk.widgets import ContentTypeSelect, ForeignKeyContentIdWidget
from django import forms
from django.contrib import admin
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import ManyToOneRel
from common.models import Buy, Sell, Rent, Advert
class AdvertModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AdvertModelForm, self).__init__(*args, **kwargs)
try:
model = self.instance.content_type.model_class()
model_key = model._meta.pk.name
except (AttributeError, ObjectDoesNotExist):
model = self.fields['content_type'].queryset[0].model_class()
model_key = 'id'
self.fields['object_id'].widget = ForeignKeyContentIdWidget(
rel=ManyToOneRel(model_key, model, 'id'),
admin_site=admin.site
)
class Meta:
model = Advert
fields = "__all__"
widgets = {
'content_type': ContentTypeSelect
}
class AdvertAdmin(admin.ModelAdmin):
form = AdvertModelForm
Registering Models
admin.site.register(Buy)
admin.site.register(Sell)
admin.site.register(Rent)
admin.site.register(Advert, AdvertAdmin)
How It Works
PrettyGFKModelAdminMixinautomates form handling for models with GFKs.ContentTypeSelectupdates object selection dynamically when the content type changes.ForeignKeyContentIdWidgetensures proper URL handling for related objects.
Screenshots
Default create form
Create form with admin_pretty_gfk
Default edit object form
Edit object form with admin_pretty_gfk
License
This project is licensed under the MIT License.
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 admin_pretty_gfk-0.0.4.tar.gz.
File metadata
- Download URL: admin_pretty_gfk-0.0.4.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f11a6ce25d1062f92427d38c13f04750884ad1c382ca5a5a2211568a7d5701e0
|
|
| MD5 |
4cfe60832c7b85ba8231561c55352e85
|
|
| BLAKE2b-256 |
844411b8c24bab824e3806597ef334aa871b1a64498dcd9689bccc1ff8265565
|
File details
Details for the file admin_pretty_gfk-0.0.4-py3-none-any.whl.
File metadata
- Download URL: admin_pretty_gfk-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c1ad715014bdee5c33434ac165eb5a3ad9821b349b15159b79fbdc51ea71379
|
|
| MD5 |
c053e9077c9a44a61f95afa2e6a8d3ce
|
|
| BLAKE2b-256 |
96d681ed8eca10c9bcb4bd0d2b8f5198eb22157881cf07ed8bd333ec948a0d7b
|