Understanding ‘GenericForeignKey’ in Django

class Ticket(models.Model):
name = models.CharField(max_length=200, verbose_name=_("name"))
slug = models.SlugField(max_length=250, null=False, blank=True, verbose_name=_("slug"))
class Project(models.Model):
name = models.CharField(max_length=200, verbose_name=_("name"))
slug = models.SlugField(max_length=250, null=False, blank=True, verbose_name=_("slug"))
class User(models.Model):
name = models.CharField(max_length=200, verbose_name=_("name"))
slug = models.SlugField(max_length=250, null=False, blank=True, verbose_name=_("slug"))
class Timeline(models.Model):
involved_object = *****
event_type = models.CharField(max_length=250, default="created")

Using ‘GenericForeignKey’:

  • Give your model a ForeignKey to ContentType. The usual name for this field is “content_type”.
  • Give your model a field that can store primary key values from the models you’ll be relating to. For most models, this means a PositiveIntegerField. The usual name for this field is “object_id”.
  • Give your model a GenericForeignKey, and pass it the names of the two fields described above. If these fields are named “content_type” and “object_id”, you can omit this — those are the default field names GenericForeignKey will look for.
class Timeline(models.Model):
content_type = models.ForeignKey(ContentType, related_name="content_type_timelines")
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
event_type = models.CharField(max_length=250, default="created")
t1 = TimeLine(content_object=project_object)
t1.save()
TimeLine.objects.filter(content_object=project_object)
# This will also fail
TimeLine.objects.get(content_object=project_object)
  • Get the ‘ContentType’ object with the following code.
  • from django.contrib.contenttypes.models import ContentType contenttype_obj = ContentType.objects.get_for_model(project_object)
  • “object_id” is stored with project_object.id
  • TimeLine.objects.filter(object_id=project_object.id, content_type=contenttype_obj)

The article was originally published at MicroPyramid blog.

--

--

--

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Compensation Logic using RabbitMQ

Understanding Python context-managers for absolute beginners

How to make hexagons with PHP

Think Twice Before You Buy a Mac

The Oculus for Business Solution

Join Our Dev Team Roundtable Discussion + 📢 Airdrop Date Announcement 📢 Get ready to mark your…

Email flooder — python

Five Best Easy Ways To Create A Data Entry Form In Excel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MicroPyramid

MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

More from Medium

Introduction to Django Framework

introduction to Django Framework

Docker zing a Python Django Web App on Ubuntu

Static files and templates in Django

User Auth with REST API in Django — Part 2