This question has been flagged
1 Reply
2889 Views

My code:

######------------------------------------------------------------------------------#########################

class DataField(models.Model)

    _name = 'data.field'

    cycle_start_1 = fields.Integer("Cycle Start 1" ,default=0)

    cycle_end_1 = fields.Integer("Cycle End 1")

    cycle_start_2 = fields.Integer("Cycle Start 2", related='cycle_end_1')

    cycle_end_2 = fields.Integer("Cycle End 2")

    cycle_start_3 = fields.Integer("Cycle Start 3", related='cycle_end_2')

    cycle_end_3 = fields.Integer("Cycle End 3")

######------------------------------------------------------------------------------#########################

Now, when I save my 1st form and go to create 2nd form, in that form the value of the field 'cycle_start_1' should be equal to the last saved field 'cycle_end_3' value.

How can I get the previously saved value into my new form?

Thanks in advance,

Kind regards,

Arpit Thakar

Avatar
Discard
Best Answer

Hi,

What you can do is that you can call a function which will return the required value to the field cycle_start_1.

@api.model
def get_last_value(self):
last_rec = self.env['data.field'].search([], order='id desc', limit=1)
if last_rec:
cycle_val = last_rec.cycle_end_3
else:
cycle_val = 0
return cycle_val

cycle_start_1 = fields.Integer("Cycle Start 1", default=get_last_value)


Thanks

Avatar
Discard
Author

Hello,

Your given solution worked well without any error. Now if I want to reset the 'cycle_start_1' to 0, where should I implement my code. For example after certain number of cycle run I want it to start again from zero, so how can I use a button to perform this in my code. Or, if there is any other method to reset 'cycle_start_1' to 0, kindly provide solution with a suitable example.

Thanks in advance.