I have a function for filling in many fields automatically with an api.onchange() decorator.
@api.onchange('nursery_morning', 'nursery_evening', 'responsible_partner')
def retrieve_responsible_nursery(self):
if self.nursery_morning or self.nursery_evening:
if self.responsible_partner:
if not self.resp_civility1 and not self.resp_name1 and not self.resp_cp1 and not self.resp_num1 \
and not self.resp_address1 and not self.resp_town1 \
and not self.resp_phone1 and not self.resp_phonemobile1:
self.resp_civility1 = self.responsible_partner.title.name
self.resp_name1 = self.responsible_partner.name
self.resp_cp1 = self.responsible_partner.zip_id.name
self.resp_num1 = self.responsible_partner.street_number_id.name
self.resp_address1 = self.responsible_partner.street_id.name
self.resp_town1 = self.responsible_partner.city_id.name
self.resp_phone1 = self.responsible_partner.phone
self.resp_phonemobile1 = self.responsible_partner.mobile
This function works, but I do not want the fields to change until the fields are saved in the database and not before.
Currently, the fields do not change once one of the fields listed are filled but not saved in database
Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).
resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")
Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.
Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:
@api.multi
def write(self, vals):
res = super(ModelName, self).write(vals)
if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
if res.nursery_morning or res.nursery_evening:
if res.responsible_partner:
if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1 \
and not res.resp_address1 and not res.resp_town1 \
and not res.resp_phone1 and not res.resp_phonemobile1:
res.resp_civility1 = res.responsible_partner.title.name
res.resp_name1 = res.responsible_partner.name
res.resp_cp1 = res.responsible_partner.zip_id.name
res.resp_num1 = res.responsible_partner.street_number_id.name
res.resp_address1 = res.responsible_partner.street_id.name
res.resp_town1 = res.responsible_partner.city_id.name
res.resp_phone1 = res.responsible_partner.phone
res.resp_phonemobile1 = res.responsible_partner.mobile
return res
This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.