Skip to main content

Description

‘django-generic-fk’ is essentially just a collection of widgets and mixin for easy use GenericForeignKey in Django admin.

Install

::

pip install generic-fk

Usage

Mixin

In file admin.py:

from django.contrib import admin

from apps.models import SomeModel
from generic_fk.mixins import ModelAdminMixin


class SomeModelAdmin(ModelAdminMixin, admin.ModelAdmin):
    pass

admin.site.register(SomeModel, SomeModelAdmin)

Widget

In file admin.py:

from django.contrib import admin
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import ManyToOneRel
from django import forms

from apps.models import SomeModel
from generic_fk.widgets import ContentTypeSelect


class SomeModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SomeModelForm, self).__init__(*args, **kwargs)
        try:
            model = self.instance.content_type.model_class()
            model_key = model._meta.pk.name
        except (AttributeError, ObjectDoesNotExist):
            model = self.fields['content_type'].queryset[0].model_class()
            model_key = 'id'
        self.fields['object_id'].widget = ForeignKeyRawIdWidget(
            rel=ManyToOneRel(model, model_key),
            admin_site=admin.site
        )

    class Meta:
        model = SomeModel
        widgets = {
            'content_type': ContentTypeSelect
        }

class SomeModelAdmin(admin.ModelAdmin):
    form = SomeModelForm

admin.site.register(SomeModel, SomeModelAdmin)

Metadata

Release files for generic-fk 0.1.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 generic-fk 0.1.1
File Size Uploaded Provenance
generic-fk-0.1.1.tar.gz 3.8 kB Without provenance Details

Release files / generic-fk-0.1.1.tar.gz

Download URL generic-fk-0.1.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bdfb362b1d9c1614d9f7653a62701a3f6623dd91bfccd9d9a2d907447d7ffcfe
BLAKE2b-256 checksum
How to use checksums
a411a5c0e3cf26f4d0c8ba55a33c8bdca0b934d3388d4d0a5ed5352b7550b739
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for generic-fk 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

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