From 09c415a4162027ba213a6e47174b954b6fea45a5 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 29 Jun 2021 17:32:51 +0300 Subject: [PATCH] fix DecimalField precision fix decimal.InvalidOperation: [] error if DecimalField param precision is zero or less --- mongoengine/fields.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 8973ac96..be07ec88 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -482,10 +482,11 @@ class DecimalField(BaseField): value = decimal.Decimal("%s" % value) except (TypeError, ValueError, decimal.InvalidOperation): return value - return value.quantize( - decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding - ) - + if self.precision > 0: + return value.quantize(decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding) + else: + return value.quantize(decimal.Decimal(), rounding=self.rounding) + def to_mongo(self, value): if value is None: return value