Odoo 8 - Compute Field with “store=True” can't store in database

主宰稳场 提交于 2019-12-02 07:52:20

问题


I'm using Odoo 8 and I have a problem with compute field with type is Many2One.

Here, I declared department_id:

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

And fuction of this compute field:

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

It seems to work right now, but it's not. In view, I can see the value of department_id. But in the database, the table has no column department_id and has no value of this column.

My question is: how can I store the department_id in database?

Notes:

  • In the declaration of department_id, I set store=True, but it did NOT store the value of this field in database.
  • I did a test. I add compute_field with type Text, It works, I don't know why compute field doesn't work with type 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:


The store=True works. It may be that you added the computation to the field after it was created on the database. In this case the initial computation is not triggered.

A work around is to drop the column from the table and then upgrade your module. When the field is recreated the initial values should be computed.



来源:https://stackoverflow.com/questions/28834086/odoo-8-compute-field-with-store-true-cant-store-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!