Search code examples
odoo

To get default values in one2many odoo 12


actually i got 2 models,A and B ,connection same as sale.order and sale.order.line ,that connecting line here is medicine_line it is a one2many field.so when adding a specialist_name in model A,"each line" in the model B's(Notebook) specialist_name wants to get by default .

I have tried below code but getting the name only on the first line of the notebook:

*.py

medicine_line = One2many field

@api.onchange('specialist_name')
def _onchange_specialist_name(self):
    for record in self:
        for each in record.medicine_line:
            if record.specialist_name:
                lines = [(5, 0, 0)]
                for line in self.specialist_name:
                    val = {
                        'specialist_name': line.id,
                    }
                    lines.append((0, 0, val))
                record.each = lines

Solution

  • You can pass the default value in the context.

    <group>
        <field name="specialist_name"/>
    </group>
    
    <field name="medicine_line" context="{'default_specialist_name': specialist_name}">
        <tree editable="bottom">
            ...
            <field name="specialist_name"/>
        </tree>
    </field>
    

    Each time you click the Add item link button the specialist name should be filled with the default value specified in the context.