[Django] #27907: DecimalFields don't work with initial

9 views
Skip to first unread message

Django

unread,
Mar 7, 2017, 4:16:21 PM3/7/17
to django-...@googlegroups.com
#27907: DecimalFields don't work with initial
--------------------------------------+------------------------
Reporter: fvox13 | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
Consider a simple form with a DecimalField:


{{{
class PatronLevelForm(forms.Form):
name = forms.CharField()
lower_threshold = forms.DecimalField(max_digits=20,
decimal_places=2)
}}}


In a view, set up some initial data:

{{{
form = PatronLevelForm(initial={'name': 'Valued Donor', 'lower_threshold':
Decimal(0.01),},)
}}}


The Decimal gets cast to float and request.POST ends up looking like this:


{{{
<QueryDict: {u'name': [u'Valued Donor'], u'lower_threshold':
[u'0.01000000000000000020816681711721685132943093776702880859375']}>
}}}

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

Django

unread,
Mar 7, 2017, 6:32:33 PM3/7/17
to django-...@googlegroups.com
#27907: DecimalFields don't work with initial
--------------------------------+--------------------------------------
Reporter: Steven L Smith | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

The issue is in your code, not in Django. You need to initialize the
`Decimal` with a string `Decimal('0.01')` rather than a float.

--
Ticket URL: <https://code.djangoproject.com/ticket/27907#comment:1>

Django

unread,
Mar 7, 2017, 9:53:49 PM3/7/17
to django-...@googlegroups.com
#27907: DecimalFields don't work with initial
--------------------------------+--------------------------------------
Reporter: Steven L Smith | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Steven L Smith):

Replying to [comment:1 Tim Graham]:


> The issue is in your code, not in Django. You need to initialize the
`Decimal` with a string `Decimal('0.01')` rather than a float.

Wow... you're absolutely right, and I'm an idiot... I can't believe that
a.) I didn't notice that; and b.) I actually submitted a ticket on it.
Sorry for wasting your time.
Need more coffee! :-)

--
Ticket URL: <https://code.djangoproject.com/ticket/27907#comment:2>

Reply all
Reply to author
Forward
0 new messages