#33954: NaN can be stored in DecimalField but cannot be retrieved
-------------------------------------+-------------------------------------
               Reporter:  xbello     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33954>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182cff99dee-00ca8ff1-e98b-461d-a6fa-1da1e6f2251f-000000%40eu-central-1.amazonses.com.

Reply via email to