Skip to main content

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 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

Using the Mixin

from admin_pretty_gfk.mixins import PrettyGFKModelAdminMixin
from django.contrib import admin

class AdvertAdmin(PrettyGFKModelAdminMixin, admin.ModelAdmin):
    pass

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.

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

admin_pretty_gfk-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

admin_pretty_gfk-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file admin_pretty_gfk-0.0.1.tar.gz.

File metadata

  • Download URL: admin_pretty_gfk-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.17

File hashes

Hashes for admin_pretty_gfk-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f30f7a48235e8d2ae284a5c1060bc3701a986b4236a0ffe4d4f175a4732a280d
MD5 3ca75766abe57c4771a2f175dfb58cd9
BLAKE2b-256 8535e47170ff59c31a62b032a8ba59ccd1fb127aa564df0bd9003fad2c163929

See more details on using hashes here.

File details

Details for the file admin_pretty_gfk-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for admin_pretty_gfk-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c65622b2735cd594a5499807bf45653c42792555b23ad9dbb9b7947df93d0ce
MD5 187d97359be3c9b55800b6834bf40975
BLAKE2b-256 a19b3f11d3c9f4d78330f6f1ea06e98761312d4be50da752bdca815b2dcaf5c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page