How to Create One2many Field in Odoo

To create One2many field in Odoo 16, involves defining a relationship between two models where one record in the parent model can be associated with multiple records in the child model. A One2many field in Odoo represents a type of relationship between two database models where a single record in the parent model can be associated with multiple records in the child model. 

How to Create One2many Field in Odoo

When you create an Order in Odoo and add multiple Order Line records to it, you're using a One2many relationship. This is a common type of relational database relationship which allows for structured and efficient data management, especially when dealing with complex data that needs to be associated in a one-to-many relationship.

How to Create One2many field in Odoo

Here's a step-by-step guide on how to create One2many field in Odoo 16:

Step 1: Define the Models

First of all, you need to have two models which are model and child model.

Example:

from odoo import models, fields

class ParentModel(models.Model):
    _name = 'parent.model'
    _description = 'Parent Model'

    name = fields.Char(string='Name')
    child_ids = fields.One2many(
        comodel_name='child.model', 
        inverse_name='parent_id', 
        string='Children'
    )

class ChildModel(models.Model):
    _name = 'child.model'
    _description = 'Child Model'

    name = fields.Char(string='Name')
    parent_id = fields.Many2one(
        comodel_name='parent.model', 
        string='Parent'
    )

Parent Model:
name: A simple Char field to hold the name of the parent.
child_ids: The One2many field that establishes the relationship with following parameters i.e. comodel_name and inverse_name. The comodel_name is the model name of the child while the inverse_name is the field name in the child model that points back to the parent.

Child Model:
name: A Char field to hold the name of the child.
parent_id: The Many2one field that connects the child to the parent model.

Step 2: Define the Views

The next step is to define the views in XML to display this relationship in Odoo 16.

<odoo>
    <record id="view_parent_model_form" model="ir.ui.view">
        <field name="name">parent.model.form</field>
        <field name="model">parent.model</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name"/>
                        <field name="child_ids">
                            <tree>
                                <field name="name"/>
                            </tree>
                            <form>
                                <field name="name"/>
                            </form>
                        </field>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="view_child_model_tree" model="ir.ui.view">
        <field name="name">child.model.tree</field>
        <field name="model">child.model</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <field name="parent_id"/>
            </tree>
        </field>
    </record>
</odoo>


Parent Form View:
Displays the parent's name field and an One2many field for child_ids.
Inside the One2many field, both the tree and form views of the child model are defined.

Child Tree View:
Displays a list view of the child records with the name and parent_id fields.

Step 3: Update database and module

After defining the models and views, update the Odoo module, and you should see the One2many relationship in action. You can now create parent records and associate multiple child records with each parent.

I hope this article will very helpful for you. For more queries please comment below or contact us.
Previous Post Next Post