Skip to Content
Menu
This question has been flagged
1 Reply
6762 Views

I am trying to get the old value of a field in onchnge method.

 Here is what I tried.

      @api.onchange('assigned_to')
     # @api.depends('assigned_to')
    def onchange_assigned_to(self):
           print('onchange_assigned_to')
           history = self._origin.read(["assigned_to"])
          if history:
             id = history[0]["assigned_to"][0]
             last_assigned = self.env['res.users'].browse([id])
             self.last_assign_id = last_assigned        

The aboe code is working and i getting the old value only if i change the field value through GUI.

I am also changing the field value via button action., that time this function is not working.

How can I achieve this?


And I also tried on compute function with @api.depends.

 That time I got an 'AttributeError: 'crm.lead' object has no attribute '_origin''

         

                                                                                 


Avatar
Discard
Best Answer

Hello,

Try in write method.

@api.multi
def write(self, vals):
if 'assigned_to' in vals:
new_value = vals['assigned_to']
old_value = self.assigned_to.id
print(new_value, old_value)
res = super(YourClass, self).write(vals)
return res

This will work once you save the record. If you want it also at the time of onchange. then use both functions.


Thanks & Regards

Avinash N K

Avatar
Discard
Related Posts Replies Views Activity
3
Oct 23
5973
1
Sep 23
1962
1
May 23
999
2
Apr 23
1374
1
Mar 23
996