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.2.tar.gz (11.0 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.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_genfkadmin-0.2.2.tar.gz
  • Upload date:
  • Size: 11.0 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.2.tar.gz
Algorithm Hash digest
SHA256 017dd4e3cf617c1a8e25a2330dc2e01d42e443ae5f7bf773d1ad349363621e8d
MD5 17b128696ecc926646c12ed97232dbc4
BLAKE2b-256 f5e8edab696d02b75df27a1625073adf5b624eb0dcd626250ca37e0b6c767f83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_genfkadmin-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe5edce491a8743876a0fcb42e258e0e907696d073b54072e98b442eef89a51
MD5 d906be0c0137523d1cce0d964b450e02
BLAKE2b-256 6e36fe14b3d9bb02c6aa4782cc9af520d10ee626c8534e151ae289a5d73908d7

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