Relationships between models
Looking again at our module design, we have these relationships:
- Each Task has a Stage. That's a many-to-one relationship, also known as a foreign key. The inverse is a one-to-many relationship, meaning that each Stage can have many Tasks.
- Each Task can have many Tags. That's a many-to-many relationship. The inverse relationship, of course, is also a many-to-many, since each Tag can be in many Tasks.
The following Entity Relationship Diagram can help visualizing the relationships we are about to create on the model. The lines ending with a triangle represent a many sides of the relationships:
Let's add the corresponding relationship fields to the to-do tasks in our todo_model.py
file:
class TodoTask(models.Model): _inherit = 'todo.task' stage_id = fields.Many2one('todo.task.stage', 'Stage') tag_ids = fields.Many2many('todo.task.tag', string='Tags')
The preceding code shows the basic syntax of these fields, setting the related...