Home › Curriculum › Odoo Developer › Relational fields: Many2one, One2many and Many2many
The ORM · 16 min · 17–20Relational fields: Many2one, One2many and Many2many
Declaring relations, ondelete, domains, commands for writing x2many values, and relation tables.
In this lesson
- The three relational fields
- Domains, context and company checks
- Commands for x2many values
Key points
- Many2one with ondelete set null/restrict/cascade
- One2many needs an inverse Many2one
- Many2many uses a relation table; name it when needed
- Command.create/update/delete/unlink/link/clear/set ↔ tuples 0–6
Real-world example
A script used Command.delete instead of Command.unlink to remove tags from contacts and deleted the tags themselves from the database. Restoring from the morning backup on a copy and re-running with unlink taught the team the difference permanently.
Questions and answers
What's the tuple equivalent of Command.set([1,2])?
(6, 0, [1, 2])
Why name a Many2many relation table explicitly?
When two Many2many fields link the same two models, or for clarity across modules.