Skip to Content
Menu
This question has been flagged
1433 Views

A little background first: I have a custom field pref_name used to store a customer's preferred name. Due to some business requirements we have to store their full name in the 'name' field of the customer record. So I overrode the name_get method to change the display name. 

However, when I show this as a many2one field in a kanban view, it shows an older version of the name. I did some digging in the js code for the many2one widget and found it grabs the name associated with the ID in the database. This name value doesn't change when I change the pref_name field, but it does change when I update the name field.


My current solution: I know this is not great, but it's the only thing I've found to work so far:


def name_get(self):
   res = []
   for record in self:
      name = record.pref_name or record.name
      res.append((record.id, name))
      return res

# Add then remove a space on the name to update the stored name value under the id in the database
@api.onchange('pref_name')
def _change_pref_name(self):
   self.update({'name': self.name + ' '})
   self.update({'name': self.name[:len(self.name)-1]})


Is there a better way to update the pair [id, name] that is stored in the database when pref_name is upated?

Avatar
Discard