Django Contenttypes: The unexplored territory
Arghya Saha (~arghya) |
Django is a great framework, easy to learn with a lot of batteries included feature. In short as the tagline states -
The Web framework for perfectionists with deadlines.
We all love django ORM and the amazing features it brings to the plate. But there is a little unexplored territory of DJango ORM, remains unknown to a many developers. It is Django Contenttype framework, which we might have heard of but never used it.
If you thought as the name suggests, it must be something to do with the asset management fields like Imagefield, etc (atleast thats what I thought when I first heard of it), then you are wrong. So lets look at an example to learn about it.
Suppose you have an application in which the user can add both Posts and Pictures. Now you want to create a feature where users can comment on the posts or pictures. So generally we think there are 2 approaches, either you create 2 different tables PostComments and PictureComments which would have respective foreign key relationships, or else you create a single table Comments which has two different columns for foreign key relationship for Post and Pictures.
Now what if I tell you there is third option.
Introducing Django Contenttype
Django contenttype is the solution where you don't need to create two different foreign keys neither use different Comment tables, rather you can take advantage of the generic interface to create link between the tables. And this is highly beneficial since tomorrow if you have another segment called Quotes then you can you the same existing table and to add comments support to the new segment. One of the biggest advantages of using contenttype framework is that it lets you use all the existing Django ORM features and everything is in the django application layer.
- Introduction about the author and description of the scenario as explained above [3-4 min]
- Different approaches which can be taken (different model, different foreign key) [ 2 min]
- Introduction to django contenttype, generic foreign key and how it can help [ 3-4 min]
- Example of how to implement django contenttype in a model [5-6 min]
- Show how to access django contenttype relationships and take advantage of the django ORM when using contenttype [8-10 min]
- Usecases for django content type [ 1-2 min ]
- Q/A [ 2 min]
Takeaways from the talk
- Understanding of django contenttype framework
- When to use contenttype framework
- Using generic foreign key / contenttype framework in django models.
- Basic web development experience.
- Understanding of django models and django ORM.
Arghya(argo) is a senior backend developer at Healthifyme. He is an IIT bombay alumni and worked with multiple frameworks like Django, Flask, Celery, Angular, ExpressJS, Jhipster, etc. He has professional experience in django for around 3 years, which includes his current organisation which heavily relies on django. During his free time he contributes to the stackoverflow community and mentors/freelances at codementor. He is also a vivid trekker who did multiple himalayan treks.