Odoo 8 - 具有“store = True”的计算字段无法存储在数据库中

时间:2015-03-03 14:18:27

标签: field store odoo openerp-8

我正在使用Odoo 8,我遇到compute field类型为Many2One的问题。

在这里,我宣布department_id

department_id = fields.Text(
    string="Department", store=True,
    comodel_name="hr.department",
    compute="_get_department_id"
)

此计算字段的功能:

@api.depends('employee_id')
def _get_department_id(self):
    if self.employee_id.department_id:
        self.department_id = self.employee_id.department_id.name

它现在似乎有效,但事实并非如此。在视图中,我可以看到department_id的值。但是在数据库中,该表没有列department_id,并且没有此列的值。

我的问题是:如何将department_id存储在数据库中?

注意:

  • department_id的声明中,我设置了store=True,但它没有将此字段的值存储在数据库中。
  • 我做了一个测试。我添加compute_field类型为Text,它有效,我不知道为什么计算字段不适用于Many2One类型。

    @api.depends('employee_id')
    def _get_compute_field(self):
    if self.employee_id.department_id:
        self.compute_field = self.employee_id.department_id.name
    
    compute_field = fields.Text(
        string="Compute Field", store=True,
        compute="_get_compute_field"
    )
    

1 个答案:

答案 0 :(得分:6)

store=True有效。 可能是您在数据库上创建计算后将其添加到字段中。在这种情况下,不会触发初始计算。

解决方法是从表中删除列,然后升级模块。重新创建字段时,应计算初始值。