Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


onchange return domain for one2many product ID Field

on 8/20/14, 6:02 AM 5,422 views

Using (Sale Order) onchange Function how to add return domain for (Sale Order Line) one2many table Product ID Field?..

In Default onchange domain return example available for same table and column.

Example: return {'domain': {'column_name': [('id', 'in', listids)]}}

But how to add domain in one2many table Field using onchange method ?..

Requirement:-  Sale Order based on Partner selection dynamic add domain in Sale Order Line Product Field.


  def onchange_partner_id(self, cr, uid, ids, part, context=None):
        res = super(sale_order, self).onchange_partner_id(cr, uid, ids, part, context=context)       
        res['value'].update({'order_line': []})
        if part:
            product_obj = self.pool.get('product.product')
            matching_cust_ids = product_obj.search(cr, uid, [('x_cust_name','=',part)])            
            res['value'].update({'custproduct_id': matching_cust_ids})   
            domain = {'domain':{'order_line.product_id': [('order_line.id', '=', matching_cust_ids)]}}    
        return res       

Issue: The above code return domain for one2many table product Field is not working.

For more background, what we were trying to do earlier (https://www.odoo.com/forum/help-1/question/limiting-customers-to-specific-products-58972) is link a customer and a product using a field on many2one field on the product page - 'x_cust_name': fields.many2one('res.partner', 'Customer', store=True). Ideally the module would only display the products that have the 'x_cust_name" equal to the name of the customer on the Sales Order.

Alex Gallien
on 8/20/14, 3:17 PM

Prakash - Janeesh's solution below worked! I think we were seriously overthinking things. Thanks again for your help, I really appreciate it.

Alex Gallien
on 8/21/14, 3:31 PM


| 4 3 4
Cochin, India

Odoo Developer

On 8/21/14, 12:55 AM

If x_cust_name is a many2one field tp partner, then you can simply give domain in xml for product_id field. Domain should be domain="[('x_cust_name', '=', parent.partner_id)]"

Ah of course it is way simpler than we were trying to make it. This worked perfectly using 'edit form view' from dev mode, but I am having trouble figuring out how to declare the xpath in my module. I am adding an answer with my code - if you could take a look I would really appreciate it.

Alex Gallien
on 8/21/14, 2:13 PM

Actually, scratch that - I figured it out. Thanks for the help!!

Alex Gallien
on 8/21/14, 3:31 PM
Alex Gallien
On 8/21/14, 2:16 PM

Janeesh - here is my attempt to implement your solution in a module. The domain isn't showing up for me right now, and I think it is because of my xpath declaration. Could someone help me figure out how to declare this domain correctly? Thanks!

EDIT: For anyone wondering the solution here, I think it had to do with the fact that the 'field' tag I was looking for was embedded past another field tag. The xpath that ended up working for me was:

 <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']"

which actually could be shortened to 

xpath expr="//tree/field[@name='product_id']"

Here is my original (not functional) code.


    <record id="view_quotation_tree" model="ir.ui.view">

        <field name="name">sale.order.form</field>

        <field name="model">sale.order</field>

        <field name="inherit_id" ref="sale.view_order_form"/>

        <field name="arch" type="xml">

           <xpath expr="//field[@name='product_id']" position="replace">

              <field name="product_id" context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}" groups="base.group_user" on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)" domain="[('x_cust_name', '=', parent.partner_id)]"/>




Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)


Asked: 8/20/14, 6:02 AM
Seen: 5422 times
Last updated: 3/16/15, 8:10 AM