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

Hi

Please how can achive this  result for the new record ( attribut a calculated value from the last record ). Thanks

Avatar
Discard
Best Answer

Hello,

Here the biggest concern would be how would you come to know which one is the last record?

The calculated fields can be added to have such calculations.

Here we can take the lastly created record as the last record based on the create_date field which is by default created in odoo.

The calculations field can be created either from the code or from the GUI by going to Settings/Technical/Database Structure/Models after activating the developer mode.

The biggest concern would be if all the fields are calculated ones and they are being calculated from the previous record's value, where will the first record will get the value?

Instead of calculated fields. I would suggest to have normal fields and fetch the default values using the default_get method. Where you will get the values from the previous record.

You can also keep it on create method which would be more accurate considering the values of the previous record because if you're using the system simultaneously with multiple users it's hard to decide which record was the last record if all the users click on create at the same time.

The code to fetch the default value from previous record is as following.

@api.model

def create(self,vals):

    curr_date = datetime.now

    cr_dt = curr_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT)

    prev_rec = self.search(['create_date','<',  cr_dt)], limit=1, order='create_date desc')

    vals['montent_octroy'] = prev_rec.montent_octroy

     return super(<class_name>,self).create(vals)

Let me know if you need more information on the same.


Avatar
Discard
Author

Hi Thanks so much for your comment,

i extended the fleet app (fleet.vehicle.log.fuel), so the biggest challenge is to attribute the value ( Le Reste ) to the new record and for the right vehicule ( for ex Audi ).

In other word the odoo should get the laste calulated value ( Le reste ) and set it on Montant octroyé for the right vehicule( audi ).

Can you please update your answer with those informations i will be very grateful