Search code examples
pythonodoo-10

Odoo 10.0 Name_search domain example


I need to use many2one product_id field in my one2many field product_lines and products should be filtered based on page_no.

Model1 contains product_lines(o2m : model2-model1_id) and page_no

class model1(models.Model):
    _name = "model1"

    ....
    page_no = fields.Integer('Page No.')
    ...
    product_lines = fields.One2many('model2', 'model1_id', 'Product lines')

model2 contains product_id and model1_id(m2o : model1)

class model2(models.Model):
    _name = "model2"

    model1_id = fields.Many2one('model1')
    product_id = fields.Many2one('product.product')

in my xml for form view of model1 is

    <field name="product_lines"  >
        <tree editable="bottom" name="petan_nos">
            <field name="model1_id" invisible="1" />
            <field name="product_id" domain="[('page_no', '=', model1_id.page_no)]" />
        </tree>
    </field>

And this is how I ma getting error front end error Uncaught Error: AttributeError: object has no attribute 'page_no' no error stack on terminal.

How to handle this and use field of relational fields in domain so I can use name_search and modify the behaviour?

Thanks in advance.


Solution

  • Inherit product.product and add field page_no.

    _inherit='product.product'
    
    page_no = fields.Char('Page_no')
    

    And also define page_no in model2

    class model2(models.Model):
    _name = "model2"
    
    model1_id = fields.Many2one('model1')
    product_id = fields.Many2one('product.product')
    page_no = fields.Char('Page_no')
    

    In xml

    <field name="product_lines"  >
        <tree editable="bottom" name="petan_nos">
            <field name="page_no" />
            <field name="model1_id" invisible="1" />
            <field name="product_id" domain="[('page_no', '=', page_no)]" />
        </tree>
    </field>
    

    Hope it will help you.