Search code examples
pythonxmlodoo

Odoo: How to use context to pass value from model to one2many field


I want to pass the value of a many2one field to a one2many field in order to filter results of the latter, i am trying to use context but i don't know if i'm using it correctly or not

The idea is that i want to filter products (part of the one2many field) based on the chosen branch (the many2one field)

my code is as below

class CustomTransRequest(models.Model):
    _name = 'custom.trans.request'

    branch_from_id = fields.Many2one('custom.branch', string="From", required=True)
    branch_to_id = fields.Many2one('custom.branch', string="To", required=True)
    product_ids = fields.One2many(comodel_name="custom.trans.line", inverse_name="request_id", string="Products", required=False, )

class CustomTransLine(models.Model):
    _name = 'custom.trans.line'

    request_id = fields.Many2one("custom.trans.request", string="Request ID", required=True, )
    product_id = fields.Many2one("custom.product", string="Product", required=True)
    qty = fields.Integer(string="Qty", required=True)

class CustomProduct(models.Model):
    _name = 'custom.product'

    product_name = fields.Char(string="Name", required=False, )
    branch_line = fields.One2many('custom.branch.line', 'product_id', string='Branch', )

class CustomBranchLine(models.Model):
    _name = 'custom.branch.line'

    branch_id = fields.Many2one('custom.branch', string='Branch', ondelete='cascade')
    product_id = fields.Many2one('custom.product', string='Product', required=True, )
    qty = fields.Integer(string="QTY", required=True, )
<record id="form_custom_trans_request" model="ir.ui.view">
        <field name="name">custom.trans.request.form</field>
        <field name="model">custom.trans.request</field>
        <field name="arch" type="xml">
            <form string="Transfer Request">
                <sheet>
                    <group>
                        <field name="branch_from_id"/>
                        <field name="branch_to_id"/>
                    </group>
                    <notebook>
                        <page string="Products" name="order_lines">
                            <field name="product_ids" context="{'default_branch_id':branch_from_id}" mode="tree">
                                <tree editable="bottom">
                                    <control>
                                        <create string="Open list view"/>
                                    </control>
                                    <field name="product_id" domain="[('branch_line.branch_id','=',default_branch_id)]" />
                                    <field name="qty"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

Solution

  • I found the answer on this website http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html