This question has been flagged

Dear all,
I am using Odoo 12, and found a problem when trying to delete several selected records from the "action" button on top. When I delete records one by one (from the action button), it works fine, but If I select several records, I get the error:

raise ValueError("Expected singleton: %s" % self)

I will try to explain what are my requirements.

1. On model_1, I need to show some records from a model_2. This is done when a user makes a selection from a Many2One field that is related to model_2.
Those records are shown on form view and if user saves the form, they are saved on model_1. If user do not save the form, the information is discarded.

In order to achieve that, I had to use "_compute_fields" on model_1 for some fields. So, the workflow is:
- User selects a record (vehicle_id field) from the many2one related field;
- The "_compute_field" function for the desired records, will get all the information required from model_2 for each field;
- The information is passed to this fields in order to be saved on the database "only if user saves the form".

So, besides other fields, my model_1 definition for the computed fields is:

    #Fields definition
    vehicle_id = fields.Many2one('mymodule.model_2', string="Vehicle", required=True)
    customer = fields.Char("Customer", readonly=True, compute='_compute_fields', store=True)
    brand = fields.Char("Brand", size=64, readonly=True, compute='_compute_fields', store=True)
    model = fields.Char("Model", size=64, readonly=True, compute='_compute_fields', store=True)
    #Compute function
    @api.multi
    @api.depends('vehicle_id')
       def _compute_fields(self):
          first_model_rec_set = self.env['mymodule.model_2'].search([('name','=',self.vehicle_id.name)])
        for rec in first_model_rec_set:
            self.customer = rec.customer_id.name
            self.brand = rec.brand_id.name
            self.model = rec.model_id.name

The error is related with this "compute function" and full error is:

Traceback (most recent call last):
  File "C:\odoo12\odoo\api.py", line 1039, in get
    value = self._data[key][field][record.id]
KeyError: 113

During handling of the above exception, another exception occurred:

"Traceback (most recent call last):
  File "C:\odoo12\odoo\fields.py", line 963, in __get__
    value = record.env.cache.get(record, self)
  File "C:\odoo12\odoo\api.py", line 1041, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('mymodule.model_1(113,).customer', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\odoo12\odoo\http.py", line 654, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\odoo12\odoo\http.py", line 312, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "C:\odoo12\odoo\tools\pycompat.py", line 87, in reraise
    raise value
  File "C:\odoo12\odoo\http.py", line 696, in dispatch
    result = self._call_function(**self.params)
  File "C:\odoo12\odoo\http.py", line 344, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\odoo12\odoo\service\model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\odoo12\odoo\http.py", line 337, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\odoo12\odoo\http.py", line 939, in __call__
    return self.method(*args, **kw)
  File "C:\odoo12\odoo\http.py", line 517, in response_wrap
    response = f(*args, **kw)
  File "c:\odoo12\addons\web\controllers\main.py", line 962, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "c:\odoo12\addons\web\controllers\main.py", line 954, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\odoo12\odoo\api.py", line 749, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "C:\odoo12\odoo\api.py", line 736, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "c:\workspace_36\odoo_12\custom_addons\mymodule\models\odometer.py", line 145, in unlink
    return super(mymodule_odometro, self).unlink()
  File "C:\odoo12\odoo\models.py", line 3141, in unlink
    self.recompute()
  File "C:\odoo12\odoo\models.py", line 5218, in recompute
    vals = {n: rec[n] for n in ns}
  File "C:\odoo12\odoo\models.py", line 5218, in <dictcomp>
    vals = {n: rec[n] for n in ns}
  File "C:\odoo12\odoo\models.py", line 5065, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "C:\odoo12\odoo\fields.py", line 967, in __get__
    self.determine_value(record)
  File "C:\odoo12\odoo\fields.py", line 1054, in determine_value
    self.compute_value(recs)  File "C:\odoo12\odoo\fields.py", line 1034, in compute_value    self._compute_value(records)  File "C:\odoo12\odoo\fields.py", line 1025, in _compute_value    getattr(records, self.compute)()
  File "c:\workspace_36\odoo_12\custom_addons\mymodule\models\odometer.py", line 77, in _compute_fields
    first_model_rec_set = self.env['mymodule.model_2'].search([('name','=',self.vehicle_id.name)])
  File "C:\odoo12\odoo\fields.py", line 961, in __get__
    record.ensure_one()
  File "C:\odoo12\odoo\models.py", line 4669, in ensure_one
    raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: mymodule.model_1(113, 112)"

How can I solve this error? Any idea about what could be the problem?

Thank you in advance.

Best regards

Avatar
Discard
Best Answer

Hi,

Update the compute function like this,

    @api.multi
    @api.depends('vehicle_id')
       def _compute_fields(self):
          for record in self:
              first_model_rec_set = self.env['mymodule.model_2'].search([('name','=',record.vehicle_id.name)])
            for rec in first_model_rec_set:
                  record.customer = rec.customer_id.name
                  record.brand = rec.brand_id.name
                  record.model = rec.model_id.name


Thanks

Avatar
Discard
Author

Thank you @Niyas, it's working as expected.

+1 for you.