ORM method on_change odoo Filed dosn't change between product_id and product_uom

519 Views Asked by At

There is my code, when I choose a product I want to change the Unit of Measure. what is the mistakes in my code below :

class Product(models.Model):
       _name = 'operation.product'

       product_id = fields.Many2one('product.product', string="Product")
       product_qty = fields.Float(digits=(6, 2), help="Quantity")
       product_uom= fields.Many2one('product.uom', string ="Unit of Measure",required=True)

   def onchange_product_id(self, cr, uid, ids, product_id, context=None):
           """ Changes UoM if product_id changes.
           @param product_id: Changed product_id
           @return:  Dictionary of changed values
           """
           res = {}
           if product_id:
               prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
               res['value'] = {
                   'product_uom': prod.uom_id.id
               }

           return res 


   operation.xml

   <form string="Product">
                    <sheet>
                        <group>
                            <field name="product_id" on_change="onchange_product_id(product_id)"/>
                            <field name="product_qty" />
                        </group>
                        <group>
                               <field name="product_uom"/>
                        </group>
                    </sheet>
                </form>

How I have to do correctly? I want to have a method with recently api8 like this:

   @api.onchange('amount', 'unit_price')
   def _onchange_price(self):
       # set auto-changing field
       self.price = self.amount * self.unit_price
       # Can optionally return a warning and domains
       return {
           'warning': {`enter code here`
               'title': "Something bad happened",
               'message': "It was very bad indeed",
           }
       }
2

There are 2 best solutions below

1
On

Here you have example of odoo8 onchange method: https://www.odoo.com/forum/help-1/question/odoo-8-api-onchange-example-how-to-copy-field-value-to-other-field-74838

Your method look fine except return part, so just remove return and that should do it.

Hope it helped :)

0
On

Actually product_uom is only required if you want to choose other uom for product else you can take related fields and make it readonly, for e.g

product_uom = fields.Many2one('product.uom', string='Unit of Measure', related='product_id.uom_id', store=True, readonly=True)

If you will take related fields then no need to write onchange for uom.

Here is the onchange method for your code.

class Product(models.Model):
    _name = 'operation.product'

    product_id = fields.Many2one('product.product', string="Product")
    product_qty = fields.Float(digits=(6, 2), help="Quantity")
    product_uom= fields.Many2one('product.uom', string ="Unit of Measure",required=True)

    @api.onchange('product_id')
    def onchange_product_id(self):
        """ Changes UoM if product_id changes.
        @param product_id: Changed product_id
        @return:  Dictionary of changed values
        """
        if self.product_id:
            self.product_uom = self.product.uom_id