<strong>serializer核心参数:(默认值)</strong>
read_only (False)
write_only (False)
required (True) #若字段在反序列中不需要,设置成false
allow_null (False) #置空
default #默认值
source (source = 'user.email')#整个对象需要被传递来确定输出表示的字段,可用于创建嵌套关系
validators,error_messages,label,help_text
initial #欲填充字段(day = serializers.DateField(initial = datetime.date.today))
style #以键值对形式出现,如输入模式为密码式,模板样式为**.html,具体style需要看<a target=_blank target="_blank" href="http://www.django-rest-framework.org/topics/html-and-forms/">HTML&Forms文档</a>
<strong>serializer fields:
Boolean</strong>
BooleanField:
NullBooleanField: None也作为有效值存储
<strong>
String:</strong>
CharField:
min_length < length < max_length,
allow_blank = True:空值为有效值,默认为False;
trim_whitespace:裁剪前后两头的空格,默认为True
allow_null 不利于allow_blank:空值有两种存储形式,可能会有微妙的bug
EmailField: 有效email地址
RegexField:
SlugField:
URLFiled:
UUIDField:
FilePathField:
IPAddressField:
<strong>Numeric:</strong>
IntegerField
FloatFiled
DecimalField
<strong>Date & Time</strong>
DateTimeField
DateField
TimeField
DurationField
<strong>Choice selection</strong>
ChoiceField
MultipleChoiceField
<strong>File upload</strong>
FileField
ImageField
<strong>Composite </strong>
ListField
DictField
JSONField
<strong>Miscellaneous</strong>
ReadOnlyField
HiddenField
ModelField
SerializerMethodField
serializer = modelClassSerializer(data = data) #创建一条新的实例
serializer = modelClassSerializer(model, data = data)#更新已经存在的model实例
serializer = modelClassSerializer(model, data = {'username': 'azhou'}, partical = True) #部分字段更新