Skip to Content
Menu
This question has been flagged
2 Replies
2648 Views

In the community version, I try to calculate the case quantity by dividing unit quantity with product packaging, with the following code: 


for record in self:

  record ['x_caseqty'] = record.product_uom_qty / record.product_packaging.qty


Then I get the error message, which seems to be dividing by zero. How can I solve this issue? Thank you!

Error:

Odoo Server Error


Traceback (most recent call last):

  File "/odoo/odoo-server/odoo/tools/safe_eval.py", line 351, in safe_eval

    return unsafe_eval (c, globals_dict, locals_dict)

  File "", line 2, in <module>

ZeroDivisionError: float division by zero


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/odoo/odoo-server/odoo/http.py", line 624, in _handle_exception

    return super (JsonRequest, self) ._ handle_exception (exception)

  File "/odoo/odoo-server/odoo/http.py", line 310, in _handle_exception

    raise pycompat.reraise (type (exception), exception, sys.exc_info () [2])

  File "/odoo/odoo-server/odoo/tools/pycompat.py", line 14, in reraise

    raise value

  File "/odoo/odoo-server/odoo/http.py", line 669, in dispatch

    result = self._call_function (** self.params)

  File "/odoo/odoo-server/odoo/http.py", line 350, in _call_function

    return checked_call (self.db, * args, ** kwargs)

  File "/odoo/odoo-server/odoo/service/model.py", line 94, in wrapper

    return f (dbname, * args, ** kwargs)

  File "/odoo/odoo-server/odoo/http.py", line 339, in checked_call

    result = self.endpoint (* a, ** kw)

  File "/odoo/odoo-server/odoo/http.py", line 915, in __call__

    return self.method (* args, ** kw)

  File "/odoo/odoo-server/odoo/http.py", line 515, in response_wrap

    response = f (* args, ** kw)

  File "/odoo/odoo-server/addons/web/controllers/main.py", line 1322, in call_kw

    return self._call_kw (model, method, args, kwargs)

  File "/odoo/odoo-server/addons/web/controllers/main.py", line 1314, in _call_kw

    return call_kw (request.env [model], method, args, kwargs)

  File "/odoo/odoo-server/odoo/api.py", line 387, in call_kw

    result = _call_kw_multi (method, model, args, kwargs)

  File "/odoo/odoo-server/odoo/api.py", line 374, in _call_kw_multi

    result = method (recs, * args, ** kwargs)

  File "/odoo/odoo-server/odoo/models.py", line 6104, in onchange

    value = record [name]

  File "/odoo/odoo-server/odoo/models.py", line 5640, in __getitem__

    return self._fields [key] .__ get __ (self, type (self))

  File "/odoo/odoo-server/odoo/fields.py", line 997, in __get__

    self.compute_value (recs)

  File "/odoo/odoo-server/odoo/fields.py", line 1111, in compute_value

    records._compute_field_value (self)

  File "/odoo/odoo-server/odoo/models.py", line 3941, in _compute_field_value

    field.compute (self)

  File "/odoo/odoo-server/odoo/addons/base/models/ir_model.py", line 33, in <lambda>

    func = lambda self: safe_eval (text, SAFE_EVAL_BASE, {'self': self}, mode = "exec")

  File "/odoo/odoo-server/odoo/tools/safe_eval.py", line 374, in safe_eval

    pycompat.reraise (ValueError, ValueError ('% s: "% s" while evaluating \ n% r'% (ustr (type (e)), ustr (e), expr)), exc_info [2])

  File "/odoo/odoo-server/odoo/tools/pycompat.py", line 13, in reraise

    raise value.with_traceback (tb)

  File "/odoo/odoo-server/odoo/tools/safe_eval.py", line 351, in safe_eval

    return unsafe_eval (c, globals_dict, locals_dict)

  File "", line 2, in <module>

ValueError: <class 'ZeroDivisionError'>: "float division by zero" while evaluating

"for record in self: \ n record ['x_caseqty'] = record.product_uom_qty / record.product_packaging.qty \ n"

Avatar
Discard
Best Answer

Try this:

for record in self:
qty = record.product_packaging.qty
record['x_caseqty'] = record.product_uom_qty / qty if qty else 0

Depending on your requirements, you may want to assign a value of None to the record['x_caseqty'], differentiating between 0 and "cannot calculate yet"

Avatar
Discard
Author Best Answer

That works! Thank you!

Avatar
Discard