Opened 3 years ago
Last modified 3 years ago
#33954 closed Cleanup/optimization
NaN can be stored in DecimalField but cannot be retrieved — at Initial Version
| Reported by: | Xabier Bello | Owned by: | nobody | 
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 4.1 | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
Same as ticket https://code.djangoproject.com/ticket/33033, but I managed to trigger it anyway:
Steps to reproduce
- Create a brand new project using python 3.10 and django 4.1 with the default sqlite3 backend.
 
- Create a model with a DecimalField: 
class MyModel(models.Model): value = models.DecimalField(max_digits=10, decimal_places=5) - Programmatically create a model instance with value="nan",
obj = MyModel.objects.create(value="nan") obj.save() - Then try to retrieve the object from the database (or refresh from database):
MyModel.objects.get(pk=1)
 
Traceback
    Traceback (most recent call last):
      File "/sandbox/dj/bug/dec/views.py", line 9, in <module>
        MyModel.objects.get(pk=1)
      File "/lib64/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/lib64/python3.10/site-packages/django/db/models/query.py", line 646, in get
        num = len(clone)
      File "/lib64/python3.10/site-packages/django/db/models/query.py", line 376, in __len__
        self._fetch_all()
      File "/lib64/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all
        self._result_cache = list(self._iterable_class(self))
      File "/lib64/python3.10/site-packages/django/db/models/query.py", line 117, in __iter__
        for row in compiler.results_iter(results):
      File "/lib64/python3.10/site-packages/django/db/models/sql/compiler.py", line 1333, in apply_converters
        value = converter(value, expression, connection)
      File "/lib64/python3.10/site-packages/django/db/backends/sqlite3/operations.py", line 344, in converter
        return create_decimal(value).quantize(
    TypeError: argument must be int or float
The value "nan" (and maybe "inf" also) skip the validation in DecimalField.to_python, because is not None, and is not instance of float. But decimal.Decimal("nan") works without triggering the exception, so NaN gets stored in the DB.
  Note:
 See   TracTickets
 for help on using tickets.