Skip to main content

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

  1. PrettyGFKModelAdminMixin automates form handling for models with GFKs.
  2. ContentTypeSelect updates object selection dynamically when the content type changes.
  3. ForeignKeyContentIdWidget ensures proper URL handling for related objects.

Screenshots

Default create form default create form Create form with admin_pretty_gfk create form with admin_pretty_gfk Default edit object form default edit object form Edit object form with admin_pretty_gfk Edit object form

License

This project is licensed under the MIT License.

Metadata

Release files for admin-pretty-gfk 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for admin-pretty-gfk 0.0.4
File Size Uploaded
admin_pretty_gfk-0.0.4.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for admin-pretty-gfk 0.0.4
File Interpreter ABI Platform
admin_pretty_gfk-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / admin_pretty_gfk-0.0.4.tar.gz

Download URL admin_pretty_gfk-0.0.4.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f11a6ce25d1062f92427d38c13f04750884ad1c382ca5a5a2211568a7d5701e0
BLAKE2b-256 checksum
How to use checksums
844411b8c24bab824e3806597ef334aa871b1a64498dcd9689bccc1ff8265565
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.17

Release files / admin_pretty_gfk-0.0.4-py3-none-any.whl

Download URL admin_pretty_gfk-0.0.4-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c1ad715014bdee5c33434ac165eb5a3ad9821b349b15159b79fbdc51ea71379
BLAKE2b-256 checksum
How to use checksums
96d681ed8eca10c9bcb4bd0d2b8f5198eb22157881cf07ed8bd333ec948a0d7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page