Skip to Content
Menu
This question has been flagged
4 Replies
5574 Views

I have 2 one2many field named "measure" in res.partner and sale.order.line .I want that when I select customer in sale order form , it record the contents of the res.partner field (measure) in the field "measure" of order line.
I can do this via the onchange method but when I save the sale order , the value of "measure" field on sale order line becomes empty. Can someone help me to solve the problem?
Here is my code 

class res_partner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'

measure= fields.One2many('order.mesure', 'partner', string='Measures') 


class Sale_order_line(models.Model): 
_name = 'sale.order.line'
_inherit = 'sale.order.line'

measure= fields.One2many('order.mesure', 'sale_line', string=" Measures",  store=True) 

@api.onchange('mesures')
def onchange_measure(self): 
   if self.order_id.partner_id:
     self.measure = self.order_id.partner_id.measure​


 
Thank you

Avatar
Discard
Best Answer

If you are using Odoo Version 11 and above then you have to give attribute force_save in XML to save the read-only fields in onchange. For the previous versions, you have to do the same function in write and create ORM Functions. So you have to override them.

eg:

<field name="x" force_save="1"/>
For previous versions:
override create and write functions and call your onchange function.
@api.model
def create(self, vals):
result = super(ClassName, self).create(vals)
result.onchange_measure()
DO same for write()

Avatar
Discard
Best Answer

Hi,  U need to use constrain instead of on change.

Avatar
Discard

You just change @api.onchange to @api.constains('mesures')

Author Best Answer

Hello,

how can i use constrain?

thank you

Avatar
Discard