Django IntegerRangeField验证失败

Django IntegerRangeField验证失败,第1张

Django IntegerRangeField验证失败

MinValuevalidator
MaxValuevalidator
是整数,所以他们是不正确的验证用在这里。而是将验证器专门用于range:
RangeMinValuevalidator
RangeMaxValuevalidator

这两个验证器都位于模块中

django.contrib.postgres.validators

这是验证程序源代码的链接。

另外,

IntegerRangeField
在Python中将an表示为
psycopg2.extras.NumericRange
对象,因此
default
在模型中指定参数时,请尝试使用an而不是字符串。

注意:

NumericRange
默认情况下,对象包含下限,不包含上限,因此NumericRange(0,100)将包括0,而不包括100。您可能希望使用NumericRange(1,101)。您也可以
bounds
NumericRange
对象中指定一个参数,以更改包含/排除的默认值,以代替更改数字值。请参阅NumericRange对象文档。

例:

# models.py filefrom django.contrib.postgres.validators import RangeMinValuevalidator, RangeMaxValuevalidatorfrom psycopg2.extras import NumericRangeclass SomeModel(models.Model):    age_range = IntegerRangeField(        default=NumericRange(1, 101),        blank=True,        validators=[ RangeMinValuevalidator(1),  RangeMaxValuevalidator(100)        ]    )


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5648447.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存