Serializer Fields

核心参数

read_only=False
write_only=False
required=True
default 指定了该选项则在没有输入值时使用,使用了该字段则意味required=False 不可与required同用
allow_null=False 允许传递None值(如果没有显式的的default的设置 在输出时会序列化None但是输入时不会自动增加None值)
source
validators
error_messages

html

label
help_text
initial在html表单中的预设值
style

字段类型

BooleanFiled

不支持allow_null

TRUE_VALUES = {
    't', 'T',
    'y', 'Y', 'yes', 'YES',
    'true', 'True', 'TRUE',
    'on', 'On', 'ON',
    '1', 1,
    True
}
FALSE_VALUES = {
    'f', 'F',
    'n', 'N', 'no', 'NO',
    'false', 'False', 'FALSE',
    'off', 'Off', 'OFF',
    '0', 0, 0.0,
    False
}

to_internal_value(data)、to_representation(value)根据上述TRUE_VALUES和FALSE_VALUES返回True False

NullBooleanField

不支持allow_null 会默认设置allow_null=True 不可改变

NULL_VALUES = {'n', 'N', 'null', 'Null', 'NULL', '', None}

CharField

allow_blank是否运行空字符串
trim_whitespace会调用strip去掉首尾的空白字符
max_length
min_length

EmailField

相比CharField多了个EmailValidator的validator

RegexField

相比CharField 接收一个regex 添加RegexValidator

SlugField URLField

限制输入的字符[a-zA-Z0-9_-]+ 或 http://<host>/<path>

UUIDField

支持一个format参数:('hex_verbose', 'hex', 'int', 'urn')
"5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
"5ce0e9a55ffa654bcee01238041fb31a"
"123456789012312313134124512351145145114"
"urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"

FilePathField

IPAddressField

IntegerField

max_value min_value

FloatField

max_value min_value

DecimalField

max_digits decimal_places coerce_to_string
max_value min_value
localize rounding

DateTimeField DateField TimeField DurationField

ChoiceField MultipleChoiceFIeld

FileFIeld ImageField

ListField DictFied JSONField HStoreField

ReadOnlyField HiddenField ModelField SerializerMethodField

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容