Skip to Content
Menu
This question has been flagged
3 Replies
5238 Views

I need to store some computation field value in Database.

In account_invoice, I have days_since_invoice compute field. 

 days_since_invoice = fields.Float(string='Days Since Invoice', compute='_compute_days_since_invoice', 

                                      readonly=True, store=True)


    @api.one

    def _compute_days_since_invoice(self):        

        today_date = dt.datetime.now().strftime('%Y-%m-%d')

        self.days_since_invoice = days_diff(self.date_invoice, today_date)


I used store=True in days_since_invoice field for storing values in database.

If I use store=True, computation function is not working. So in db wrong values are stored. 

If i not use store=True, computation function is working properly. 

I need to store this field value in db for reporting purpose. Because i am looping the invoices for generating report. It takes lot of time to generate. And also proxy error comes while generating report. If this compute field value in db means, I use query to generate the report. 

so Any body give some idea to store compute field value in database.

Avatar
Discard
Best Answer

Hi Priyait,

If you want to store your compute field and want it to be calculated, you should use "@api.depends() " which will trigger the function when the fields value will be changed which are defined in api.depends.

If you want to compute days_since_invoice field daily, then you should create another compute field to get current date and add this field in depends of _compute_days_since_invoice() method

Ex:

current_date = fields.Date(compute='_get_current_date')

@api.multi
def _get_current_date(self):
    for rec in self:
        rec.current_date = dt.datetime.now().strftime('%Y-%m-%d')

@api.depends('date_invoice', 'current_date')
def _compute_days_since_invoice(self):
    today_date = dt.datetime.now().strftime('%Y-%m-%d')
    self.days_since_invoice = days_diff(self.date_invoice, today_date)


Avatar
Discard

Hello, I'm trying to achieve the same goal but from UI, so I go to Settings>Technical>Fields and create corresponding fields for corresponding model; so in account.invocie I have created x_date_today to compute datetime.datetime.today() everyday just to recompute an stored value.

The problem I saw is that even tho it changed, for whatever reason, its not making my other field that has it dependancy to recompute, then if I put store=true to computed date for today, it changes and works.

Is there something i'm missing?

Best Answer

Hi.

Give @api.depends decorator for the function and see.


@api.one
@api.depends('date_invoice')
def _compute_days_since_invoice(self):
today_date = dt.datetime.now().strftime('%Y-%m-%d')
self.days_since_invoice = days_diff(self.date_invoice, today_date)


Thanks

Avatar
Discard
Author

Hi,

I will use @api.depends. I need a clarification.

days_since_invoice field value changed daily. Eg: Its value is 8, then tomorrow its value will be 9 and day by day this value will be changed. So this value to be updated in database on a daily basis.

If i use this code @api.depends('date_invoice')

whether days_since_invoice field value stored and updated on daily basis in db or not?