Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
5775 มุมมอง

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.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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)


อวตาร
ละทิ้ง

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?

คำตอบที่ดีที่สุด

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

อวตาร
ละทิ้ง
ผู้เขียน

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?