DecimalField

356 views
Skip to first unread message

Derrick Jackson

unread,
Aug 25, 2013, 5:47:00 AM8/25/13
to django...@googlegroups.com
Hi All,

My Model has a DecimalField:      

amount = models.DecimalField(max_digits=19, decimal_places=2, verbose_name='Amount')

My ModelForm has the following:  

self.fields['amount'] = forms.DecimalField(error_messages={
            'required': 'Amount is required.',
            'invalid': 'Numbers only.',
            'max_decimal_places': 'Only 2 decimal places.'})

However the max_decimal_places validation check does not seem to be working.  Is there a certain way I need to use this that will properly allow the error to be thrown?

Thomas Scrace

unread,
Aug 25, 2013, 6:04:56 AM8/25/13
to django...@googlegroups.com
You're creating an entirely new DecimalField, rather than altering the error messages of your existing field.

I think what you want is:

class  YourForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.fields["amount"].error_messages= {
            'required': 'Amount is required.',
            'invalid': 'Numbers only.',
            'max_decimal_places': 'Only 2 decimal places.'}

Tom


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Derrick Jackson

unread,
Aug 27, 2013, 9:43:59 AM8/27/13
to django...@googlegroups.com
Thanks Tom.  I actually tried that at one point and this is the error I received:  not all arguments converted during string formatting.

The required and invalid error message keys work fine but it appears the max_decimal_places key is being ignored. Has anyone seen this before?  BTW, I am using Django 1.5.2

Derrick Jackson

unread,
Nov 5, 2013, 11:17:04 AM11/5/13
to django...@googlegroups.com
Morning all,

More information which may suggest I need to enter a bug with the Django team.  

If I remove my own custom error_messages dictionary for the Decimal Field in my ModelForm all validation works as it should using the default error messages built into Django.

Tom Evans

unread,
Nov 5, 2013, 12:20:30 PM11/5/13
to django...@googlegroups.com
On Tue, Aug 27, 2013 at 2:43 PM, Derrick Jackson
<derrick....@gmail.com> wrote:
> Thanks Tom. I actually tried that at one point and this is the error I
> received: not all arguments converted during string formatting.

To fix your problem, ensure that your error messages have the same
format specifiers as the defaults, which are here:

'invalid': _('Enter a number.'),
'max_value': _('Ensure this value is less than or equal to
%(limit_value)s.'),
'min_value': _('Ensure this value is greater than or equal to
%(limit_value)s.'),
'max_digits': _('Ensure that there are no more than %s digits
in total.'),
'max_decimal_places': _('Ensure that there are no more than %s
decimal places.'),
'max_whole_digits': _('Ensure that there are no more than %s
digits before the decimal point.')


The problem arises because the documentation seems incorrect for 1.5
(and trunk):

"""
The max_value and min_value error messages may contain
%(limit_value)s, which will be substituted by the appropriate limit.
"""

https://docs.djangoproject.com/en/1.5/ref/forms/fields/#decimalfield

where as the code indicates that min_value, max_value, max_digits,
max_decimal_places and max_whole_digits all take a format argument:

https://github.com/django/django/blob/stable/1.5.x/django/forms/fields.py#L260

Furthermore, the docs say "may contain", which is very misleading, as
the OP has shown, the qualifier has to be "MUST contain" - there is
nothing optional about it:

> $ python
Python 2.7.5 (default, Sep 13 2013, 10:44:18)
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import forms
>>> class BadForm(forms.Form):
... dec = forms.DecimalField(max_digits=3,
error_messages={'max_digits':'too many digits'})
...
>>> b=BadForm({'dec': '12.3456789'})
>>> b.is_valid()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 126, in is_valid
return self.is_bound and not bool(self.errors)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 117, in _get_errors
self.full_clean()
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 272, in full_clean
self._clean_fields()
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 287, in _clean_fields
value = field.clean(value)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/fields.py",
line 155, in clean
self.validate(value)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/fields.py",
line 320, in validate
raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
TypeError: not all arguments converted during string formatting



Cheers

Tom

Derrick Jackson

unread,
Nov 5, 2013, 3:30:14 PM11/5/13
to django...@googlegroups.com
Thanks Tom.  This did the trick.  Much appreciated.


On Sunday, August 25, 2013 5:47:00 AM UTC-4, Derrick Jackson wrote:
Reply all
Reply to author
Forward
0 new messages