Ajuda

0

Expected singleton

Avatar
Pamela Castaneda

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
                  Descartar
                  3Respostas
                  1
                  Avatar
                  Niyas Raphy
                  Melhor Resposta

                  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

                  1Comentário
                  Avatar
                  Descartar
                  Avatar
                  Pamela Castaneda
                  -

                  Thanks Niyas Raphy, this works fine

                  2
                  Avatar
                  Baiju KS
                  Melhor Resposta

                  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.
                  1Comentário
                  Avatar
                  Descartar
                  Avatar
                  Pamela Castaneda
                  -

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

                  0
                  Avatar
                  Russ Schneider
                  Melhor Resposta

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


                  Avatar
                  Descartar