Skip to main content

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.

  1. Create a subclass of GenericFKModelForm for your model.
  2. Create a subclass of GenericFKAdmin for your model.
  3. ???
  4. 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

example

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(MarketingMaterial)
class MarketingMaterialAdmin(GenericFKAdmin):
    form = MarketingMaterialAdminForm

    def get_form(self, request, obj=None, change=False, **kwargs):
        if obj:
            self.form = partial(
                MarketingMaterialAdminForm,
                filter_callback=lambda queryset: queryset.filter(customer=obj.customer),
            )
        else:
            # this is important, otherwise, 1. add -> 2. change -> 3. add
            # will use the filter on 2. in 3.
            self.form = MarketingMaterialAdmin
        return super().get_form(request, obj=obj, change=change, **kwargs)

Now when loading an existing MarketingMaterial, the content_object options are filtered by the chosen Customer example

A complete example django app exists in this repository at here

Roadmap

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

django_genfkadmin-0.1.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

django_genfkadmin-0.1.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file django_genfkadmin-0.1.2.tar.gz.

File metadata

  • Download URL: django_genfkadmin-0.1.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for django_genfkadmin-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7fe26ea16d1702215d0018ef7894efb21364015def68a40cd434ecffeda61480
MD5 c5f4c23c0649809905efba9f7e794d63
BLAKE2b-256 8b3fa69dbf51003fc045138ffa78c1038cb9e8f566bbe990c024a32b3dd55c93

See more details on using hashes here.

File details

Details for the file django_genfkadmin-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_genfkadmin-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 791bd9ef5e98e84c0863de0cfa5219132525c2d473c749800965f164ade572a5
MD5 8d4e1a33dfb014d97ea2c7d3ad64379c
BLAKE2b-256 b00f52def657aca8efe1c441c91a6ec9c61c97bba8246e35ba0eebcb503ad58a

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