Fixed -- Prevented models.DecimalField from accepting NaN, Inf, and -Inf values.
@@ -2,7 +2,6 @@ import collections.abc
import copy
import datetime
import decimal
-import math
import operator
import uuid
import warnings
@@ -1703,22 +1702,24 @@ class DecimalField(Field):
def to_python(self, value):
if value is None:
return value
+ try:
if isinstance(value, float):
- if math.isnan(value):
+ decimal_value = self.context.create_decimal_from_float(value)
+ else:
+ decimal_value = decimal.Decimal(value)
+ except (decimal.InvalidOperation, TypeError, ValueError):
raise exceptions.ValidationError(
self.error_messages["invalid"],
code="invalid",
params={"value": value},
)
- return self.context.create_decimal_from_float(value)
- try:
- return decimal.Decimal(value)
- except (decimal.InvalidOperation, TypeError, ValueError):
+ if not decimal_value.is_finite():
raise exceptions.ValidationError(
self.error_messages["invalid"],
code="invalid",
params={"value": value},
)
+ return decimal_value
def get_db_prep_save(self, value, connection):
return connection.ops.adapt_decimalfield_value(
