This question has been flagged
2 Replies
4745 Views

I have a simple onchange function in which I am writing to a few fields. But the value returned as "vals" isn't getting to the frontend. The values returned in the network tab for Here is the function:

	@api.multi
	@api.onchange('email')
	def onchange_email(self):
		vals = {}
		contactid = ""
		siteid = ""
		respartner = self.env['res.partner'].search([('id','=',self._origin.id)])
		for partner in respartner:
			for site in partner.company_site_lines:
				if site.company_corporate:
					site.email = self.email.upper()
					site.write({"email":self.email.upper()})
			for contact in partner.contact_ids:
				if contact.flag_first_contact:
					contactid = contact.id
					contact.email = self.email.upper()
					contact.site_email = self.email.upper()
					contact.write({'email':self.email.upper(),'site_email':self.email.upper()})
					self.write({'contact_ids':[(1,contact.id,{'email':self.email.upper(),'site_email':self.email.upper()})]})
			partner.email = self.email.upper()
			partner.write({'email':self.email.upper()})
			vals['email'] = partner.email
			vals['active_tabs'] = partner.active_tabs
			ressite = self.env['company.site'].search([('company_site_id','=',partner.id)])
			vals['contact_ids'] = self._origin.contact_ids
                        #Printing vals['contact_ids'] gives the proper one2many field with the correct id: partner.contact(475,)                         #But the output in chrome is: # {"jsonrpc": "2.0", # "id": 427040437, # "result": { # "value": { # "contact_ids": [ # [5] # ], # .
#                .
#                .
#                .     return {'value':vals} # Is there something wrong with the way I am returning Values?

Avatar
Discard
Author

@Prakash, (Didn't have enough karma to comment.) Yes, that can be done, but that is not what I was looking for. I was looking to return the vals.

Author Best Answer

The reason this wasn't happening was that I was trying to save inside an onchange. The onchange function, even if you write into the object, doesn't actually completely write into the database. The issue is related to a concept called dirty fields. When you have a dirty field (i.e any field that is affected by the onchange function) before saving, Odoo doesn't support updating x2Many fields. This is why the fields weren't being passed on to the UI. 
A workaround I had for this is by making a javascript file, including the formview object and modifying the way saving works in Odoo. 
This essentially saves and performs a .reload() function which loads all the values. This modified save and .reload() function is placed inside the success function of the (Javascript) Formview onchange method. This solves my purpose, but may not solve everyone's. This issue is still in the wishlist in the official odoo repository. So it may be supported in future versions. But as of now, in odoo 8,9,10 and 11. The updating of x2Many fields in odoo through an onchange function is not supported. The comments (in odoo10) are in the odoo/models.py file on line 5511-5519. Hope this helps someone. 

Avatar
Discard
Best Answer

Hello Aditya , onchange method/ function are return the domain then reflection in UI.

Like: 

    @api.onchange('product_id')

    def _onchange_product_id(self):

        domain = {}

            domain['uom_id'] = "Domain....."

        return {'domain': domain}

Avatar
Discard