DecimalField Validation in Django

后端 未结 1 1008
难免孤独
难免孤独 2021-01-25 08:08

I\'m trying to validate a DecimalField in Django

Trying in shell:

>> from django.forms import DecimalField
>> from django.core.v         


        
1条回答
  •  不知归路
    2021-01-25 08:57

    You are 1 problem in field declaration. if max_digits = 2 and decimal_places = 1, your max is 9.9 without the intervention of validators.

    max_digits = 3 and validators for max = 10.0

    Use clean() function:

    >> from django.forms import DecimalField
    >> from django.core.validators import MaxValueValidator
    >> f = DecimalField(max_digits=3, decimal_places=1, validators=[MaxValueValidator(10)])
    >> f.clean(11.2)
    

    https://docs.djangoproject.com/en/1.10/ref/forms/validation/

    0 讨论(0)
提交回复
热议问题