2

I'm trying to compute a value to be displayed in a tree view, the problem is that my private function never gets executed and not setting the value for my computed field.

I've simplified the following code:

class ProjectProject(models.Model):
    _inherit = "project.project"
    assigned = fields.Char(string='Assigned multi', compute='_roles_assigned', store=False)

    @api.multi
    @api.depends('task_ids')
    def _roles_assigned(self):
        #do dome calculations
        assigned = ' test of 1' #'0 / {total}'.format(total=total)
        return assigned

tree view

as you see in the image the value is always blank

pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • What you're doing is directly setting value to the field, but it will not work as your compute will be for all the records, you have to make a loop and assign value to every record. check my code below and find answer. – Juhil Somaiya Feb 28 '20 at 05:15
  • 1
    He isn't assigning the value to anything, but yes your answer is correct. A little hint for Odoo 13: you have to assign values to every recordset the compute method is called with. In earlier versions that was not needed, but in Odoo 13 you will get Errors when not doing so. – CZoellner Feb 28 '20 at 08:42
  • Ah and to the both answerers: this is a very good question, so why no upvote for it? – CZoellner Feb 28 '20 at 08:43
  • Well, he needs to fill-up those values but he hasn't assigned any values to the field that's why it's not showing up in the tree view. @jack https://www.odoo.com/documentation/12.0/reference/orm.html#computed-fields – Juhil Somaiya Feb 28 '20 at 08:55

2 Answers2

5

When we display computed field in tree view, it will have multiple records set. So we have to set value for each record set.

Try with following code:

@api.multi
def _roles_assigned(self):
    #do dome calculations
    for record in self:
        assigned = ' test of 1' #'0 / {total}'.format(total=total)
        record.assigned = assigned
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
2

You have to iterate over the records and assign some value to it, check the code below.

@api.multi
def _roles_assigned(self):
    for rec in self:
        rec.assigned = 'assign your value here'
Juhil Somaiya
  • 873
  • 7
  • 20
  • 1
    thanks for the answer, sice both have a similar approach I'm just setting as an answer the first response, I appreciatte the colaboration – pedrommuller Feb 28 '20 at 14:18