Reputation: 1126
I have this function that allocates multiple bonuses to employee contracts.
My particular problem is that when I call this function, it throws the error, Expected singleton failed to unpack multiple records ...
The contract_ids
am using is a many2many field with relation to the model hr.contract.
I understand the cause of a singleton error but am using a for loop for every create call so I do not know where my error is.
def action_allocate_bonus(self):
for rec in self.contract_ids:
vals = {
'contract_id': rec.id,
'cash_allowance_id': self.cash_allowance_id.id,
'computation': 'fixed',
'fixed': rec.allowance_amount,
}
self.env['ke.cash_allowances'].sudo().create(vals)
This is the actual log error:
Odoo Server Error
Traceback (most recent call last):
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5000, in ensure_one
_id, = self._ids
ValueError: too many values to unpack (expected 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 624, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/home/b14/odoo-dev/odoo13/odoo/tools/pycompat.py", line 14, in reraise
raise value
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 669, in dispatch
result = self._call_function(**self.params)
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 350, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/b14/odoo-dev/odoo13/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 339, in checked_call
result = self.endpoint(*a, **kw)
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 915, in __call__
return self.method(*args, **kw)
File "/home/b14/odoo-dev/odoo13/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/home/b14/odoo-dev/odoo13/addons/web/controllers/main.py", line 1326, in call_button
action = self._call_kw(model, method, args, kwargs)
File "/home/b14/odoo-dev/odoo13/addons/web/controllers/main.py", line 1314, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/b14/odoo-dev/odoo13/odoo/api.py", line 388, in call_kw
model.flush()
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5389, in flush
self.recompute()
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5808, in recompute
process(field)
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5792, in process
recs.mapped(field.name)
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5206, in mapped
recs = recs._mapped_func(operator.itemgetter(name))
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5165, in _mapped_func
vals = [func(rec) for rec in self]
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5165, in <listcomp>
vals = [func(rec) for rec in self]
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5616, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/home/b14/odoo-dev/odoo13/odoo/fields.py", line 997, in __get__
self.compute_value(recs)
File "/home/b14/odoo-dev/odoo13/odoo/fields.py", line 1111, in compute_value
records._compute_field_value(self)
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 3915, in _compute_field_value
getattr(self, field.compute)()
File "/home/b14/odoo-dev/odoo13/custom-addons/hr_ke/models/payroll.py", line 485, in compute_cash_allowance
if self.computation == 'fixed':
File "/home/b14/odoo-dev/odoo13/odoo/fields.py", line 984, in __get__
record.ensure_one()
File "/home/b14/odoo-dev/odoo13/odoo/models.py", line 5003, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: ke.cash_allowances(33, 34)
The code being referenced in the payroll models is this
@api.depends('computation', 'fixed')
def compute_deduction(self):
if self.computation == 'fixed':
self.amount = self.fixed
elif self.computation == 'formula':
baselocaldict = {
'result': None,
'employee': self.employee_id,
'deduction': self}
localdict = dict(baselocaldict)
try:
Eval(self.formula, localdict, mode='exec', nocopy=True)
except BaseException:
raise ValidationError(
_('Error in the formula defined for this\
deduction: %s\n [%s].') %
(self.name, self.formula))
self.amount = localdict['result']
else:
self.amount = 0.00
Upvotes: 0
Views: 275
Reputation: 125
Seems like it's "self" that's causing the error, being a set of multiple records but being referred to as being a single record.
More specifically in the first line of compute_deduction
it already hits an issue where it doesn't know what the computation
field should return for 2 different fields.
I think you might want to put the entire method in a for loop and then use the record instead of self.
like this:
@api.depends('computation', 'fixed')
def compute_deduction(self):
for record in self:
if record.computation == 'fixed':
record.amount = record.fixed
....
Upvotes: 1