Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27907 closed Bug (invalid)

DecimalFields don't work with initial

Reported by: Steven L Smith Owned by: nobody
Component: Forms Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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']}>

Change History (2)

comment:1 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

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.

in reply to:  1 comment:2 by Steven L Smith, 8 years ago

Replying to 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! :-)

Note: See TracTickets for help on using tickets.
Back to Top