简体   繁体   中英

Why store compute field odoo10 is null in database

I would like to store field tot_comp_survey into table survey_survey in odoo.
With this code:

from odoo import api, fields, models

class Survey(models.Model):
    _inherit = 'survey.survey'

    tot_comp_survey = fields.Integer("Number of completed surveys", compute="_compute_survey_statistic", store=True)


_compute_survey_statistic :

    @api.multi
    def _compute_survey_statistic(self):
        UserInput = self.env['survey.user_input']

        sent_survey = UserInput.search([('survey_id', 'in', self.ids), ('type', '=', 'link')])
        start_survey = UserInput.search(['&', ('survey_id', 'in', self.ids), '|', ('state', '=', 'skip'), ('state', '=', 'done')])
        complete_survey = UserInput.search([('survey_id', 'in', self.ids), ('state', '=', 'done')])

        for survey in self:
            survey.tot_sent_survey = len(sent_survey.filtered(lambda user_input: user_input.survey_id == survey))
            survey.tot_start_survey = len(start_survey.filtered(lambda user_input: user_input.survey_id == survey))
            survey.tot_comp_survey = len(complete_survey.filtered(lambda user_input: user_input.survey_id == survey))

and the results like this: 在此处输入图像描述

store field that I want exactly store in database, but when I apply this code into database with 1000 rows, tot_comp_survey is null in database. 在此处输入图像描述

Could anyone help me, please?
Is there something missing?
Just additional information: when I upgrade this code in small database (only 8 rows data) upgrade proccess time is normal, but when I do it in big database (1000 and more rows data) the upgrade proccess is very slow.

Basically its computed when needed. Have you tried to access any of the records?

Try to add @api.depends('user_input_ids', 'user_input_ids.input_type', 'user_input_ids.state') to the function header. Then Odoo knows when to change the values.

You may need to add some One2many fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM