Expected singleton: salesman.period.kpi.line

Viewed 29

I have this error Expected singleton: salesman.period.kpi.line

this is my py file

class KpiLine(models.Model):
    _name='salesman.period.kpi.line'
    kpi_by_hr = fields.Float('KPI By Header')
    check_field_line = fields.Boolean('Check', compute='get_user_line')
    
    def get_user_line(self):
        for line in self:
            if self.env.user == line.kpi_id.user_id:
                self.check_field_line = False
            else:
                self.check_field_line = True

This is my xml

<field name="kpi_by_hr" attrs="{'readonly': [('check_field_line', '!=', True)]}" />

My goal is to make kpi_by_hr readonly only when the creator open the document, but not for others

1 Answers

You're already looping on self to get line as Singleton. So assign your values to line not self

# wrong
self.check_field_line = False
# right
line.check_field_line = False

self can and mostly will be a multi recordset in compute method calls, and until Odoo 15 there is no way to assign a value to fields of a multi recordset, not using update() or write(). But both shouldn't be used in compute methods.

Edit: try to stick to Odoo's naming guideline and name the compute method after its computed field (if not computing multiple fields): _compute_check_field_line.

Related