Skip to main content

Django application for GenericForeignKey

Project description


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


pip install generic-fk



In file

from django.contrib import admin

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

class SomeModelAdmin(ModelAdminMixin, admin.ModelAdmin):
    pass, SomeModelAdmin)


In file

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)
            model = self.instance.content_type.model_class()
            model_key =
        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),

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

class SomeModelAdmin(admin.ModelAdmin):
    form = SomeModelForm, SomeModelAdmin)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for generic-fk, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size generic-fk-0.1.1.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page