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(MarketingMaterial)
class MarketingMaterialAdmin(GenericFKAdmin):
    form = MarketingMaterialAdminForm

    def get_form(self, request, obj=None, change=False, **kwargs):
        if obj:
            self.form = partial(
                MarketingMaterialAdminForm,
                filter_callback=lambda queryset: queryset.filter(customer=obj.customer),
            )
        else:
            # this is important, otherwise, 1. add -> 2. change -> 3. add
            # will use the filter on 2. in 3.
            self.form = MarketingMaterialAdmin
        return super().get_form(request, obj=obj, change=change, **kwargs)

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_genfkadmin-0.1.3.tar.gz
  • Upload date:
  • Size: 8.2 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.3.tar.gz
Algorithm Hash digest
SHA256 8e87d2d9f613454c88c75533eb2bdad1d7ffd5265bbfc117d0f530af70b4b51e
MD5 ac0c7b391351a40a0c5047ae0d098e61
BLAKE2b-256 2c55399326c0973fecddc2802b860b00072e5085a33c153002e045b810d09796

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_genfkadmin-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6da9f77a3ec56deec51bd31c1e665e705861f6de36edd746b431b97876fe1e79
MD5 50551ebaa9d304d2c9a64be90adc680f
BLAKE2b-256 0f30ced8f41b46c9513f8e97cd878a9a40acd2ff9293ea39176a645a225e33f9

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