- Django 3 Web Development Cookbook
- Aidas Bendoraitis Jake Kronika
- 86字
- 2025-04-04 13:15:07
Creating a model mixin to handle generic relations
Aside from normal database relationships, such as a foreign-key relationship or a many-to-many relationship, Django has a mechanism to relate a model to an instance of any other model. This concept is called generic relations. For each generic relation, we save the content type of the related model as well as the ID of the instance of that model.
In this recipe, we will look at how to abstract the creation of generic relations in the model mixins.