#30290: Documentation should make clear that values on DecimalField is better 
to be
passed as strings
-----------------------------------------+------------------------
               Reporter:  kantale        |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Documentation  |        Version:  2.1
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  1
                  UI/UX:  0              |
-----------------------------------------+------------------------
 On the documentation of
 
[https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.DecimalField
 DecimalField] it does not mention that passing values as string instead of
 float has a significant difference. A user that tests the code in the
 documentation might come to a surprise to find that the following part
 raises a ValidationError:

 {{{
 class A(models.Model):
         # A DecimalField as mentioned on the doc
         # "For example, to store numbers up to 999 with a resolution of 2
 decimal places, you’d use:"
         f = models.DecimalField(max_digits=5, decimal_places=2)
 }}}

 {{{
 A(f=10.1).clean_fields()  # This raises the following exception:
 django.core.exceptions.ValidationError: {'f': ['Ensure that there are no
 more than 2 decimal places.']}
 }}}

 If the value of f is passed as string, then it works as expected.
 {{{
 A(f="10.1").clean_fields()
 }}}

 There is a very subtle reference about this on the documentation of the
 [https://docs.python.org/3/library/decimal.html decimal module]:
 {{{
 >>> Decimal(3.14)
 Decimal('3.140000000000000124344978758017532527446746826171875')
 }}}

 See also discussion in SO: https://stackoverflow.com/questions/52376693
 /django-strange-decimalfield-validation

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30290>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.7625d1cc6ffa10eea5557ca22ac75eea%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to