Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Expected singleton: salesman.period.kpi.line

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading