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

@admin.register(Pet)
class PetAdmin(GenericFKAdmin):
    pass

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

    def filter_callback(
        self,
        queryset: QuerySet,
        obj: MarketingMaterial | None = None,
    ):
        if obj:
            return queryset.filter(customer=obj.customer)
        return queryset

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

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.2.1.tar.gz (10.8 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.2.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_genfkadmin-0.2.1.tar.gz
  • Upload date:
  • Size: 10.8 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.2.1.tar.gz
Algorithm Hash digest
SHA256 09da48d09ad067e29bb30ebcb6b0fa50ba54ad0562dce8954edbfc7688bcda49
MD5 98270da68493b4529bbf5925801eedf7
BLAKE2b-256 9165af5effe572679ed05341ce21746b7622dc241b07c43bc44c283215d92226

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_genfkadmin-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d58eeb63d2ddff75ca2a2837662161091aef9dffacbee15653227b5abb0415c9
MD5 d7b1d28f8cb72efad45628a53ae45b44
BLAKE2b-256 9b6edfbd7ac405fa8bf287a7ace3871693c5de0f3ab3e24f3905876197cde853

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