Skip to Content
Menu
This question has been flagged
3 Replies
4038 Views

I have this function that does its job, but i tried to make the field store=True and when i update it gives me and expected singleton error Expected singleton: project.project(2, 3, 1).

 this is my code.

sum_pre = fields.Float(string="Total Presupuesto Aprobado", required=False, compute='presupuesto_sum')

@api.depends('tasks.sum_total_cost')
def presupuesto_sum(self):
    etapa = self.env['project.task'].search([('project_id', '=', self.id), ('stage_id', '=', 'Presupuesto Aprobado')])
    for data in etapa:
        if data:
            self.sum_pre = round(sum(etapa.mapped('sum_total_cost')), 2)

Someone knows how to resolve it or where i have the error, Thanks!

                  Avatar
                  Discard
                  Best Answer

                  Hi,

                  Can you update the function with a for loop like this and see,

                  def presupuesto_sum(self):
                  for rec in self:
                  etapa = self.env['project.task'].search(
                  [('project_id', '=', rec.id), ('stage_id', '=', 'Presupuesto Aprobado')])
                  for data in etapa:
                  if data:
                  rec.sum_pre = round(sum(etapa.mapped('sum_total_cost')), 2)

                  Thanks

                  Avatar
                  Discard
                  Author

                  Thanks Niyas Raphy, this works fine

                  Best Answer

                  Hi Pamela Castaneda,

                  Now you are using the "sum_pre" field in the form view only, right?

                  In this case without attribute "store=True", it's calculating the value on the fly when you are opening the form. In this case, the self in

                  etapa = self.env['project.task'].search([('project_id', '=', self.id), ('stage_id', '=', 'Presupuesto Aprobado')])

                  will be a single record of the current form. So it will work fine.

                  But when you give the attribute "store=True" and update, it will try to compute and store the value of field "sum_pre" in all the existing records at once, now the self in the above code will be a recordset and it will throw the Expected singleton error as it is expecting a single record in self.

                  To avoid this you have to iterate the self, using the for loop like

                          for project in self:
                  You will get the same error if you use the "sum_pre" field in tree view without attribute "store=True"

                  Hope this helps.
                  Avatar
                  Discard
                  Author

                  Thanks for the explanation, it is very useful information for me.

                  Best Answer

                  Somewhere you're pulling in three project ids.  Some variable.  Perhaps sum_total_cost?


                  Avatar
                  Discard
                  Related Posts Replies Views Activity
                  2
                  Nov 22
                  4362
                  1
                  Jun 22
                  6759
                  1
                  Sep 21
                  2037
                  0
                  Aug 21
                  95
                  0
                  Jul 21
                  172