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

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.3.tar.gz (5.5 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.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: admin_pretty_gfk-0.0.3.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

Hashes for admin_pretty_gfk-0.0.3.tar.gz
Algorithm Hash digest
SHA256 447246a329f8f528acdbb76be61f48459668eff1b0fd91088ac655c506f57b89
MD5 56dbf18e5542f52ee5158b9e84afcdc1
BLAKE2b-256 e48dd7d5afade6432d84f8420b59ea024596a13cd6f0b71ab82ffb5381a0cd76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for admin_pretty_gfk-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cde14ed26707382bbb460639a922ad17e0a4653e4a2d7b6c8bf4982bf254bb41
MD5 308261d9e6549563b27d639cd2d0668e
BLAKE2b-256 14ef84c7ce9d9bcf2b64f86d24f48d95199969cb89a052d58e55f497c738ed20

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