This question has been flagged
2 Replies
13670 Views

I have an onchange method describe in the model and based on that values i want to store values in the read-only fields, its happen buts while saving its gone.
######################################

@api.onchange('product_id')

def onchange_product_id(self):

cr = self.env.cr

desc = ''

uom = -1

for rec in self:

cr.execute('''SELECT

product_uom.id AS uom,

product_template.name AS desc,

product_hsn_tax_code.hsn_name AS hsn_code,

product_hsn_tax_code.tax_percentage AS gst_value

FROM

product_uom JOIN

product_template ON

product_uom.id=product_template.uom_id JOIN

product_product ON

product_template.id=product_product.product_tmpl_id JOIN

product_hsn_tax_code ON

product_hsn_tax_code.id=product_template.hsn_id

WHERE

product_product.id = cast(%s AS INTEGER)''', ((rec.product_id.id),))

for line in cr.dictfetchall():

uom = line['uom']

desc = line['desc']

return {'value' :{

'uom_id':uom,

'description':desc,

}}

########################################

Avatar
Discard
Best Answer

onchange method doesn't store the read-only field values on the database.To do so you have to update the values dict of create and write methods. So better option is to store value inside the fields dict before the actual create method invokes.

override create and write function and do the same operation or store the value on the read-only field.


Avatar
Discard

Hi Hilar,

can you help how to update the values dict? What would the the overridden write function look like?

Best Answer

You can do this using overriding create and write method of odoo. Here the question is why we need to override these (create, write and unlink) methods.

The answer is to add or remove additional functionality we use overriding. For example if I want to check some constraints before creating, editing or deleting our record than we may use Odoo override methods.

There are basically three methods which we can override.

  1. Create

  2. Write

  3. Unlink


To complete code and code description read: http://learnopenerp.blogspot.com/2017/11/how-to-override-odoo-functions.html


Avatar
Discard