Skip to main content

A small toolset that helps you to work with Django's generic relations

Project description Wheel Status Supported Python versions Supported Django versions

The application provides some syntax sugar for working with Django’s generic relations


Just install the package from PyPI within pip

pip install django-generic-helpers

…or pipenv

pipenv install django-generic-helpers

…or even poetry

poetry add django-generic-helpers

That’s all. No need to add this into INSTALLED_APPS of your project or something like that.


That’s how did you work with generic relations before:

from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models

class Post(models.Model):

class Image(models.Model):
     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
     object_id = models.IntegerField()
     content_object = GenericForeignKey(ct_field='content_type', fk_field='object_id')

# Example of filtering
post = Post.objects.get(pk=1)
images = Image.objects.filter(

Looks verbose a bit, yep? Let’s rewrite this with django-generic-helpers

from django.db import models
from generic_helpers.fields import GenericRelationField

class Post(models.Model):

class Image(models.Model):
     content_object = GenericRelationField()

# Example of filtering
post = Post.objects.get(pk=1)
images = Image.objects.filter(content_object=post)

Personally, I found it much simpler and cleaner.

Features the application provides:

  • Creating an arbitrary number of generic relation fields, both required and optional;
  • Providing custom names for content_type and object_id columns
  • You can define a whitelist (or a black one) of models that could (not) be written into the field

Please, follow up the documentation for details.


A few words if you plan to send a PR:

  • Please, write tests!
  • Follow PEP-0008 codestyle recommendations.
  • When pushing, please wait while Travis CI will finish his useful work and complete the build. And if the build fails, please fix the issues before PR
  • And of course, don’t forget to add yourself into the authors list ;)


The license is MIT.

Download files

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

Files for django-generic-helpers, version 1.0.5
Filename, size File type Python version Upload date Hashes
Filename, size django_generic_helpers-1.0.5-py2.py3-none-any.whl (10.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django-generic-helpers-1.0.5.tar.gz (9.7 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