restframework 验证字段

字段及默认错误信息

Field(基类)

{
    'required': _('This field is required.'),
    'null': _('This field may not be null.')
}

BooleanField

{
    'invalid': _('Must be a valid boolean.')
}

CharField

{
    'invalid': _('Not a valid string.'),
    'blank': _('This field may not be blank.'),
    'max_length': _('Ensure this field has no more than {max_length} characters.'),
    'min_length': _('Ensure this field has at least {min_length} characters.'),
}

EmailField

{
    'invalid': _('Enter a valid email address.')
}

RegexField

{
    'invalid': _('This value does not match the required pattern.')
}

SlugField

{
    'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'),
    'invalid_unicode': _('Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.')
}

URLField

{
    'invalid': _('Enter a valid URL.')
}

UUIDField

{
    'invalid': _('Must be a valid UUID.'),
}

IPAddressField

{
    'invalid': _('Enter a valid IPv4 or IPv6 address.'),
}

IntegerField

{
    'invalid': _('A valid integer is required.'),
    'max_value': _('Ensure this value is less than or equal to {max_value}.'),
    'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
    'max_string_length': _('String value too large.')
}

FloatField

{
    'invalid': _('A valid number is required.'),
    'max_value': _('Ensure this value is less than or equal to {max_value}.'),
    'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
    'max_string_length': _('String value too large.')
}

DecimalField

{
    'invalid': _('A valid number is required.'),
    'max_value': _('Ensure this value is less than or equal to {max_value}.'),
    'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
    'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'),
    'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'),
    'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'),
    'max_string_length': _('String value too large.')
}

DateTimeField

{
    'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'),
    'date': _('Expected a datetime but got a date.'),
    'make_aware': _('Invalid datetime for the timezone "{timezone}".'),
    'overflow': _('Datetime value out of range.')
}

DateField

{
    'invalid': _('Date has wrong format. Use one of these formats instead: {format}.'),
    'datetime': _('Expected a date but got a datetime.'),
}

TimeField

{
    'invalid': _('Time has wrong format. Use one of these formats instead: {format}.'),
}

DurationField

{
    'invalid': _('Duration has wrong format. Use one of these formats instead: {format}.'),
    'max_value': _('Ensure this value is less than or equal to {max_value}.'),
    'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
}

ChoiceField

{
    'invalid_choice': _('"{input}" is not a valid choice.')
}

MultipleChoiceField

{
    'invalid_choice': _('"{input}" is not a valid choice.'),
    'not_a_list': _('Expected a list of items but got type "{input_type}".'),
    'empty': _('This selection may not be empty.')
}

FilePathField

{
    'invalid_choice': _('"{input}" is not a valid path choice.')
}

FileField

{
    'required': _('No file was submitted.'),
    'invalid': _('The submitted data was not a file. Check the encoding type on the form.'),
    'no_name': _('No filename could be determined.'),
    'empty': _('The submitted file is empty.'),
    'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'),
}

ImageField

{
    'invalid_image': _(
        'Upload a valid image. The file you uploaded was either not an image or a corrupted image.'
    ),
}

ListField

{
    'not_a_list': _('Expected a list of items but got type "{input_type}".'),
    'empty': _('This list may not be empty.'),
    'min_length': _('Ensure this field has at least {min_length} elements.'),
    'max_length': _('Ensure this field has no more than {max_length} elements.')
}

DictField

{
    'not_a_dict': _('Expected a dictionary of items but got type "{input_type}".'),
    'empty': _('This dictionary may not be empty.'),
}

JSONField

{
    'invalid': _('Value must be valid JSON.')
}

ModelField

{
    'max_length': _('Ensure this field has no more than {max_length} characters.'),
}

字段参数

选项参数

参数名称 作用
max_length 最大长度
min_lenght 最小长度
allow_blank 是否允许为空
trim_whitespace 是否截断空白字符
max_value 最小值
min_value 最大值

通用参数

参数名称 说明
read_only 表明该字段仅用于序列化输出,默认False
write_only 表明该字段仅用于反序列化输入,不用做序列化的输出,也就是只用于数据校验,默认False
required 表明该字段在反序列化时必须输入,默认True
default 反序列化时使用的默认值
allow_null 表明该字段是否允许传入None,默认False
validators 该字段使用的验证器
error_messages 包含错误编号与错误信息的字典
label 用于HTML展示API页面时,显示的字段名称
help_text 用于HTML展示API页面时,显示的字段帮助提示信息
posted @ 2023-03-05 20:25  转角90  阅读(64)  评论(0编辑  收藏  举报