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.0.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.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_genfkadmin-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 90455329c7d736fe4779f2ee5b98d1cb6396899525aa390dc23b9185ed6177f6
MD5 30fe54b61141c462701796eeaf7ef1b3
BLAKE2b-256 543eebabad89e4c87cad233f75aff410c7531743ef0702ed79ecc16f217fe938

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_genfkadmin-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffb247b9eaf1be22fcc96fb50b64d49af03a37dea3701345e6456fe307d8f1dd
MD5 9d12c3e1735dfd2688c6dfa950814acd
BLAKE2b-256 80bd44d34ad003084a4712c95bfef04646036ad1a08092278bdeaa2958925cc6

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