Serializer字段和选项
生活随笔
收集整理的這篇文章主要介紹了
Serializer字段和选项
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
字段 字段構(gòu)造方式
BooleanField BooleanField()
NullBooleanField NullBooleanField()
CharField CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)
EmailField EmailField(max_length=None, min_length=None, allow_blank=False)
RegexField RegexField(regex, max_length=None, min_length=None, allow_blank=False)
SlugField SlugField(maxlength=50, min_length=None, allow_blank=False) 正則字段,驗(yàn)證正則模式 [a-zA-Z0-9-]+
URLField URLField(max_length=200, min_length=None, allow_blank=False)
UUIDField UUIDField(format='hex_verbose') format: 1)'hex_verbose'如"5ce0e9a5-5ffa-654b-cee0-1238041fb31a" 2)'hex'如"5ce0e9a55ffa654bcee01238041fb31a" 3)'int'- 如:"123456789012312313134124512351145145114" 4)'urn'如:"urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
IPAddressField IPAddressField(protocol='both', unpack_ipv4=False, **options)
IntegerField IntegerField(max_value=None, min_value=None)
FloatField FloatField(max_value=None, min_value=None)
DecimalField DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None) max_digits: 最多位數(shù) decimal_palces: 小數(shù)點(diǎn)位置
DateTimeField DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None)
DateField DateField(format=api_settings.DATE_FORMAT, input_formats=None)
TimeField TimeField(format=api_settings.TIME_FORMAT, input_formats=None)
DurationField DurationField()
ChoiceField ChoiceField(choices) choices與Django的用法相同
MultipleChoiceField MultipleChoiceField(choices)
FileField FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ImageField ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ListField ListField(child=, min_length=None, max_length=None)
DictField DictField(child=) 選項(xiàng)參數(shù):參數(shù)名稱 作用
max_length 最大長(zhǎng)度
min_lenght 最小長(zhǎng)度
allow_blank 是否允許為空
trim_whitespace 是否截?cái)嗫瞻鬃址?max_value 最大值
min_value 最小值 通用參數(shù):
參數(shù)名稱 說(shuō)明
read_only 表明該字段僅用于序列化輸出,默認(rèn)False
write_only 表明該字段僅用于反序列化輸入,默認(rèn)False
required 表明該字段在反序列化時(shí)必須輸入,默認(rèn)True
default 反序列化時(shí)使用的默認(rèn)值
allow_null 表明該字段是否允許傳入None,默認(rèn)False
validators 該字段使用的驗(yàn)證器
error_messages 包含錯(cuò)誤編號(hào)與錯(cuò)誤信息的字典
label 用于HTML展示API頁(yè)面時(shí),顯示的字段名稱
help_text 用于HTML展示API頁(yè)面時(shí),顯示的字段幫助提示信息
?
轉(zhuǎn)載于:https://www.cnblogs.com/liangliangzz/p/10606785.html
總結(jié)
以上是生活随笔為你收集整理的Serializer字段和选项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 软工大二下半年第四周学习进度
- 下一篇: IDEA换行CRLF, LF, CR的解