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

2019-02-28 16:16发布

问题:

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.