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(Pet)
class PetAdmin(GenericFKAdmin):
    form = PetAdminForm

    def get_form(self, request, obj=None, change=False, **kwargs):
        if obj:
            self.form = partial(
                PetAdminForm,
                filter_callback=lambda queryset: queryset.filter(
                    tags__owner=obj.owner
                ),
            )
        return super().get_form(request, obj=obj, change=change, **kwargs)

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_genfkadmin-0.1.1.tar.gz
  • Upload date:
  • Size: 7.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.1.tar.gz
Algorithm Hash digest
SHA256 bd6cb0eb10c5d382a5165a7bb7cb098a507d880babce96c50269cece586533d2
MD5 6053600f06203b865f08d805723cf5fa
BLAKE2b-256 90775bfdf0b5572d399ce8a99e5fd59f7514678a56078f78b5e596eec0be611a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_genfkadmin-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0d5be4975135dc394109d0e3d0e988de0d11043062eea3afd6e7888234e20a1
MD5 652bd1041baa1acf72845db3e2054db5
BLAKE2b-256 dd3c12eb6e648b7f32a0fc4fbdbcfd3ed4f5232ef3337cc354e708a77c8aef3d

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