的
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) ] )



