Creating a relational category model
In our previous recipe, we created a simple product model, which had a couple of fields. In practice, however, applications are much more complex and have various relationships between their tables. These relationships can be one-to-one, one-to-many, many-to-one, or many-to-many. In this recipe, we will try to understand some of these relationships with the help of an example.
How to do it...
Let’s say we want to have product categories where each category can have multiple products, but each product should have only one category. Let’s do this by modifying some files from the application in the last recipe. We will make modifications to both models and views. In models, we will add a Category
model, and, in views, we will add new methods to handle category-related calls and also modify the existing methods to accommodate the newly added feature.
First, modify the models.py
file to add the Category
model and make some modifications...