One-to-many inverse relationships

An inverse of a Many2one can be added to the other end of the relationship. This has no impact on the actual database structure, but allows us to easily browse from the one side of the many related records. A typical use case is the relationship between a document header and its lines.

In our example, a One2many inverse relationship on stages allows us to easily list all the tasks in that stage. The code to add this inverse relationship to stages is as follows:

class Stage(models.Model):
    _name = 'todo.task.stage'
    # Stage class relationship with Tasks
    task_ids = fields.One2many(
        'todo.task',  # related model
        'stage_id',   # field for "this" on related model
        'Tasks in this stage')

One2many accepts three positional ...

Get Odoo 11 Development Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.