Generic Relations
Source: https://simpleisbetterthancomplex.com/tutorial/2016/10/13/how-to-use-generic-relations.html
Built in app that keeps track of models from the installed apps of your Django application.
Use case: create generic relationships between models.
Installation
Make sure you have it in your INSTALLED_APPS:
INSTALLED_APPS = [
...
'django.contrib.contenttypes',
...
]Example Scenario
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:
class Activity(models.Model):
FAVORITE = 'F'
LIKE = 'L'
UP_VOTE = 'U'
DOWN_VOTE = 'D'
ACTIVITY_TYPES = (
(FAVORITE, 'Favorite'),
(LIKE, 'Like'),
(UP_VOTE, 'Up Vote'),
(DOWN_VOTE, 'Down Vote'),
)
user = models.ForeignKey(User)
activity_type = models.CharField(max_length=1, choices=ACTIVITY_TYPES)
date = models.DateTimeField(auto_now_add=True)
post = models.ForeignKey(Post, null=True)
question = models.ForeignKey(Question, null=True)
answer = models.ForeignKey(Answer, null=True)
comment = models.ForeignKey(Comment, null=True)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
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
Adds an extra layer of complexity and will eventually make things slower.
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. Example:
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.
Last updated