File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -261,3 +261,4 @@ that much better:
261261 * Felix Schultheiß (https://github.com/felix-smashdocs)
262262 * Jan Stein (https://github.com/janste63)
263263 * Timothé Perez (https://github.com/AchilleAsh)
264+ * oleksandr-l5 (https://github.com/oleksandr-l5)
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ Development
99- (Fill this out as you fix issues and develop your features).
1010- EnumField improvements: now `choices ` limits the values of an enum to allow
1111- Fix deepcopy of EmbeddedDocument #2202
12+ - Fix error when using precision=0 with DecimalField #2535
1213
1314Changes in 0.23.1
1415===========
Original file line number Diff line number Diff line change @@ -468,6 +468,10 @@ def __init__(
468468 self .min_value = min_value
469469 self .max_value = max_value
470470 self .force_string = force_string
471+
472+ if precision < 0 or not isinstance (precision , int ):
473+ self .error ("precision must be a positive integer" )
474+
471475 self .precision = precision
472476 self .rounding = rounding
473477
@@ -482,9 +486,12 @@ def to_python(self, value):
482486 value = decimal .Decimal ("%s" % value )
483487 except (TypeError , ValueError , decimal .InvalidOperation ):
484488 return value
485- return value .quantize (
486- decimal .Decimal (".%s" % ("0" * self .precision )), rounding = self .rounding
487- )
489+ if self .precision > 0 :
490+ return value .quantize (
491+ decimal .Decimal (".%s" % ("0" * self .precision )), rounding = self .rounding
492+ )
493+ else :
494+ return value .quantize (decimal .Decimal (), rounding = self .rounding )
488495
489496 def to_mongo (self , value ):
490497 if value is None :
Original file line number Diff line number Diff line change @@ -118,3 +118,23 @@ class Person(Document):
118118 assert 2 == Person .objects (money__gt = "7" ).count ()
119119
120120 assert 3 == Person .objects (money__gte = "7" ).count ()
121+
122+ def test_precision_0 (self ):
123+ """prevent regression of a bug that was raising an exception when using precision=0"""
124+
125+ class TestDoc (Document ):
126+ d = DecimalField (precision = 0 )
127+
128+ TestDoc .drop_collection ()
129+
130+ td = TestDoc (d = Decimal ("12.00032678131263" ))
131+ assert td .d == Decimal ("12" )
132+
133+ def test_precision_negative_raise (self ):
134+ """prevent regression of a bug that was raising an exception when using precision=0"""
135+ with pytest .raises (
136+ ValidationError , match = "precision must be a positive integer"
137+ ):
138+
139+ class TestDoc (Document ):
140+ dneg = DecimalField (precision = - 1 )
You can’t perform that action at this time.
0 commit comments