Rent my time and knowledge so I can help with your project!

Using django generic relations

Let's say that we are going to build an application for storing data about celestial bodies, we start creating a model for each body that we want to represent, and later we decide that our system must have the ability to save notes for each one of those bodies. If we have many bodies, adding a new model covering the notes feature for each model would be to much dry:

Generic relations - as the name implies - will let us relate generic objects of type Star, Planet, Moon (or any other); to objects of type Note. It works using the content_type of any object and their pk as GenericForeignKey for a Note:

The benefits, are really straight:

  • in real life we will end with less code.
  • most changes to our note feature will be applied only in one place.
  • any model in our application will be able to use the note feature.
  • we will benefit of loose coupling.

In practice, if we want to add a note to Sun, we do:

>>> sun = Star.objects.get(pk=1)
>>> new_note = Note(content_object=sun, note='This is our star...')

or something like this:

>>> sun = Star.objects.get(pk=1)
>>> sun.get_content_type()
>>> 23
>>> new_note = Note(content_type=23, object_id=1 note='This is our star...')

later, if we want to retrieve all notes for planet earth, we do:

>>> earth = Planet.objects.get(pk=2)
>>> Note.objects.filter(content_object=earth)
>>> [Note-1, Note-2, Note-3]

Our example was a note system, but using this concept we can also implement features like Attachments and Tags. The original documentation has more details about using generic relations, and a particularly useful section covering reverse generic relations, that will let us do reverse lookups of objects like this:

>>> earth = Planet.objects.get(pk=2)
>>> earth.notes.all()
>>> [Note-1, Note-2, Note-3]

by Ricardo Pascal on Feb. 22, 2015

comments powered by Disqus