[英]Why store compute field odoo10 is null in database
I would like to store field tot_comp_survey
into table survey_survey
in odoo.我想将字段
tot_comp_survey
存储到 odoo 中的表survey_survey
中。
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
: _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.我想要完全存储在数据库中的存储字段,但是当我将此代码应用到具有 1000 行的数据库中时,
tot_comp_survey
是数据库中的 null。
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.只是附加信息:当我在小数据库(只有 8 行数据)中升级此代码时,升级过程时间是正常的,但是当我在大数据库(1000 行及更多行数据)中升级时,升级过程非常慢。
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.尝试将
@api.depends('user_input_ids', 'user_input_ids.input_type', 'user_input_ids.state')
添加到function header。 Then Odoo knows when to change the values.然后 Odoo 知道何时更改值。
You may need to add some One2many
fields您可能需要添加一些
One2many
字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.