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))
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