Skip to Content
Menu
This question has been flagged
2 Replies
1296 Views
odoo 14

@api.onchange('date', 'Units_ids')
def _get_day_of_date(self):
for r in self:
if r.date:
selected = fields.Datetime.from_string(r.date)
r.date_day = calendar.day_name[selected.weekday()].lower()
if r.Units_ids.dayoff == calendar.day_name[selected.weekday()].lower():
raise UserError(
_(F'This Day( {calendar.day_name[selected.weekday()]} ) Is Day Off For This Unit , Please Choose Another Unit!'))

the full erorr


Traceback (most recent call last):
  File "/home/ali/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
    result = request.dispatch()
  File "/home/ali/odoo/odoo/http.py", line 683, in dispatch
    result = self._call_function(**self.params)
  File "/home/ali/odoo/odoo/http.py", line 359, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/ali/odoo/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/ali/odoo/odoo/http.py", line 347, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/ali/odoo/odoo/http.py", line 912, in __call__
    return self.method(*args, **kw)
  File "/home/ali/odoo/odoo/http.py", line 531, in response_wrap
    response = f(*args, **kw)
  File "/home/ali/odoo/addons/web/controllers/main.py", line 1389, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/ali/odoo/addons/web/controllers/main.py", line 1381, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/ali/odoo/odoo/api.py", line 399, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/home/ali/odoo/odoo/api.py", line 386, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/home/ali/odoo/odoo/models.py", line 6272, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "/home/ali/odoo/odoo/models.py", line 6014, in _onchange_eval
    method_res = method(self)
  File "/home/ali/odoo/mydev/egyptpost_it/models/monthplan.py", line 25, in _get_day_of_date
    if r.Units_ids.dayoff == calendar.day_name[selected.weekday()].lower():
  File "/home/ali/odoo/odoo/fields.py", line 965, in __get__
    record.ensure_one()
  File "/home/ali/odoo/odoo/models.py", line 5019, in ensure_one
    raise ValueError("Expected singleton: %s" % self)
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ali/odoo/odoo/http.py", line 639, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/ali/odoo/odoo/http.py", line 315, in _handle_exception
    raise exception.with_traceback(None) from new_cause
ValueError: Expected singleton: it.units(, )
Avatar
Discard
Best Answer

Hi,
This error is caused because of multiple values/null values in an instance(Units_ids), thus here we need to loop this field and then we can check the condition; 

Please try this.

@api.onchange('date', 'Units_ids')
def _get_day_of_date(self):
for r in self:
if r.date:
    selected = fields.Datetime.from_string(r.date)
    r.date_day = calendar.day_name[selected.weekday()].lower()
    dayoff = r.Units_ids.mapped('dayoff')
    for record in dayoff:
    if record ==calendar.day_name[selected.weekday()].lower():
    raise UserError(
    _(F'This Day( {calendar.day_name[selected.weekday()]} ) Is Day Off For This Unit , Please Choose Another Unit!'))

Regards

Avatar
Discard
Author Best Answer

thank you its working and i will study it 

Avatar
Discard