You probably have already seen Django’s ContentTypes and wondered how to use it or what is it for anyway.
Basically it’s a built in app that keeps track of models from the installed apps of your Django application. And one
of the use cases of the ContentTypes is to create generic relationships between models. That’s what this post is
about.
Installation
If you didn’t remove anything from settings generated by the django-admin startproject myproject command, it’s probably
already installed. Also Django’s built in authentication system relies on it.
Basically just make sure you have it in your INSTALLED_APPS:
Example Scenario
Let’s say we have one social app where the users can ask and answer questions, up vote, down vote, favorite
a question, like a post in the website, etc.
To keep track of that we create a model named Activity. See below:
So an Activity can possibly interact with a Post, Question, Answer or a Comment instance. In a
practical scenario an Activity instance would represent a single interaction. For example, the User with ID 1
up voted a Question with ID 125:
And if I wanted to calculate how many up votes the Question 125 received, I could do something like that:
In a similar way we could work with the Post, Answer and Comment models.
Using the Generic Relations
If we wanted to achieve the same result using Generic Relations, here is what the Activity models should
look like:
Now we are no longer keeping ForeignKey to other models we want to track the favorite, like, up vote and down vote
activities. Meaning we can now track those activities to any model we want without having to modify the Activity model.
The relation is created in the model you want to track the Activity:
This also enables you to define a more meaningful name for the relations. For example, the Users can only interact
with Post and Comment models to like it. While with the Answer model, they can only
up vote/down vote. And finally with the Question model, the Users can up vote/down vote and
favorite it.
Now if you want to like a Post, you could do something like this:
A good thing about it is that if have a new model that you wants to interact with Activity, you simply add a
GenericRelation:
And it’s already ready to use:
Reverse relations
You may also define an extra parameter in the GenericRelation:
Then you can use it to query for example all favorited pictures that was uploaded by a given user:
Caveats
This is just one of the usages of the ContentTypes and GenericRelations. Even though it seems very nice to use,
take care when implementing it! It adds an extra layer of complexity and will eventually make things slower.
Another caveat is that the GenericForeignKey does not accept an on_delete argument to customize this behavior.
The default behavior will cascade all the relations.
One way to avoid the default behavior is to not define a GenericRelation. Check the example below:
Now to get the list of likes this Comment received you must use the ContentType class:
This is also an option if you want to interact with a model from Django’s contrib module or any third party model that
you don’t have access to add a GenericRelation.