Skip to main content

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 define a method on your admin class 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

Release files for django-genfkadmin 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-genfkadmin 0.3.1
File Size Uploaded
django_genfkadmin-0.3.1.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-genfkadmin 0.3.1
File Interpreter ABI Platform
django_genfkadmin-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / django_genfkadmin-0.3.1.tar.gz

Download URL django_genfkadmin-0.3.1.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7915555dd3ed2c0a6ca3b6a36157983223a705989671efa90cfec3c646899cfd
BLAKE2b-256 checksum
How to use checksums
a6cb895dbc8984b0ec1243cda9cc9c2e79e5a171aa2b4e25bcbde14dc57ca6e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / django_genfkadmin-0.3.1-py3-none-any.whl

Download URL django_genfkadmin-0.3.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9996f8241a8b539cd7d4360963ceb342140a705e98cdba9a28c41ad5e43f2610
BLAKE2b-256 checksum
How to use checksums
42c6dbf3d8404e07f2d374beceb5257083375c21f814a3b9aa479b946b2ced45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release history Release notifications | RSS feed

0.3.2

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page