Sunday, August 25, 2013

Re: DecimalField

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


On 25 Aug 2013, at 10:47, Derrick Jackson <derrick.jackson73@gmail.com> wrote:

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?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment