This question has been flagged
7 Replies
17473 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.

Code:

  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)]}}    
            res['value']['order_line'].append(domain)             
        return res       

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

Avatar
Discard

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.

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

Best Answer

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

Avatar
Discard

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.

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

Best Answer

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

Avatar
Discard
Best Answer

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)]"/>

           </xpath>

        </field>

     </record>

Avatar
Discard