我正在尝试在 Django 中验证 DecimalField
在 shell 中尝试:
>> from django.forms import DecimalField
>> from django.core.validators import MaxValueValidator
>> f = DecimalField(max_digits=2, decimal_places=1, validators=[MaxValueValidator(10)])
>> f.validate(11) # after this I should get an error
>>
但它不显示验证错误。它应该抛出一个错误,因为 11 大于 10,对吗?
我做错了什么?
最佳答案
您在字段声明中有 1 个问题。 如果 max_digits = 2 且decimal_places = 1,则在没有验证器干预的情况下,您的最大值为 9.9。
max_digits = 3 和 max = 10.0 的验证器
使用clean()函数:
>> 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/
关于python - Django 中的 DecimalField 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322802/