Professional Documents
Culture Documents
How To Use Django's Generic Relations
How To Use Django's Generic Relations
I'm a passionate software developer and researcher. I write about Python, Django and
Web Development on a weekly basis. Read more.
TUTORIAL
Your new
development career
awaits. Check out the
latest listings.
ads via Carbon
(Picture: https://www.pexels.com/photo/business-identity-blank-stationery-set-on-wood-background-6372/)
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.
INSTALLED_APPS = [
...
'django.contrib.contenttypes',
...
]
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.
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:
question = Question.objects.get(pk=125)
up_votes = question.activity_set.filter(activity_type=Activity.UP_VOTE)
In a similar way we could work with the Post, Answer and Comment models.
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)
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:
class Post(models.Model):
...
likes = GenericRelation(Activity)
class Question(models.Model):
...
activities = GenericRelation(Activity)
class Answer(models.Model):
...
votes = GenericRelation(Activity)
class Comment(models.Model):
...
likes = GenericRelation(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:
class Picture(models.Model):
user = models.ForeignKey(User)
picture_file = models.ImageField(upload_to='uploads/pictures')
date = models.DateTimeField(auto_now_add=True)
favorites = GenericRelation(Activity)
picture = Picture.objects.get(pk=1)
picture.favorites.create(activity_type=Activity.FAVORITE, user=request.user)
picture.favorites.count()
Reverse relations
You may also define an extra parameter in the GenericRelation:
class Picture(models.Model):
user = models.ForeignKey(User)
picture_file = models.ImageField(upload_to='uploads/pictures')
date = models.DateTimeField(auto_now_add=True)
favorites = GenericRelation(Activity, related_query_name='pictures')
Then you can use it to query for example all favorited pictures that was uploaded by a given user:
user = User.objects.get(pk=1)
Activity.objects.filter(pictures__user=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:
class Comment(models.Model):
text = models.CharField(max_length=500, blank=True)
date = models.DateTimeField(auto_now_add=True)
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.
Related Posts
Popular Posts
© 2015-2021 simple complex cc by-nc-sa 3.0 // about contact faq cookies privacy policy