This question has been flagged
6 Replies
22043 Views

I have many2one field and I have written onchange method on it. When onchange happens i want the previous value of it. How can it possible?

Example:

1. I selected product "A1".

2.  Then, I removed "A1" I selected product "A2".  

3. I want "A1" in method  which i selected in 1st step.

Avatar
Discard
Best Answer

Hello Rakesh,


please see this answer:

http://www.odoo.com/forum/help-1/question/how-to-get-record-s-id-on-on-change-of-wizard-s-field-126036#answer-128511 


it will help you.


Thanks

Avatar
Discard
Best Answer

The answer you will find in that link Sunny posted is to use self._origin to get the records as it is in the database (before any changes/saves was made in the gui)

Avatar
Discard
Best Answer

Hello

You need to create 2 extra fields to achieve this things.

Hope below Example help you.

Define fields

product_id = fields.Many2one('product.product',  'Product') # This standard field.

product_id_new = fields.Many2one('product.product',  'Product New') # This custom field.

product_id_old = fields.Many2one('product.product',  'Product Old') # This custom field.

Define onchange method

@api.onchange('product_id')

def onchange_product_id(self):

    OldProduct = self.product_id_new.id
    self.product_id_new = self.product_id.id
    self.product_id_old = OldProduct

you can get old product on product_id_old fields.

 Best Thanks,
Ankit H Gandhi.

Avatar
Discard
Best Answer

Hai Rakesh Yadav,

,

you can search the same record.

     obj = self.env['model.model'].search([('id', '=', self.id)])

obj.field_name give you the first value.
and self.field_name give the new value.
this is just an idea. please try this, it may help you,
thanks.

Avatar
Discard
Best Answer

Dear Rakesh,


1-Try to make another field by the same type just in different name.

2-when you change the value of first field get the old value from the second field and update it by the new value.

 

field1 = fields.Char()

field2 = fields.Char()
@api.onchange('field1')
def onchange_field1(self):

     old_value = self.field2 

     self.field2= self.field1

 

 hope I helped You...



Avatar
Discard

Thank you

Your suggestion very help full me

+1 from me