How to Create one2many Fields in Odoo
A one2many field represents a one-to-many relationship between two models in Odoo. One record in the current model can be related to multiple records in another model.
class MainModels(models.Model): _name = 'main.models' the_field = fields.One2many("comodels.of.main", "model_id") class ComodelsOfMain(models.Model): _name = 'comodels.of.main' model_id = fields.Many2one('main.models')
Key points about one2many fields:
- They require a corresponding many2one field in the related model
- The field itself doesn't store any data - it's a virtual relationship based on the many2one field in the related model
- They're commonly used for showing related records in views, especially form views where you can see and edit child records
- The field name typically ends with '_ids' by convention
- In the UI, they're often displayed as editable lists or kanban views embedded within a form
Common use cases include:
- Companies having multiple employees
- Sales orders having multiple order lines
- Products having multiple variants
- Invoices having multiple invoice lines