字段 | 字段构造方式 |
---|---|
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) 正则字段,验证正则模式 [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: 最多位数 decimal_palces: 小数点位置 |
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=) |
- CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)
?? ?max_length :校验,最大长度
? ? min_length:校验最短长度
? ? allow_blank:是否允许为空
? ? trim_whitespace:去掉前后的空白- DateTimeField(format=api_settings.DATETIME_FORMAT)
?? ?format:格式化成的样子- IntegerField(max_value=None, min_value=None)
?? ?max_value :数字
? ? min_value:数字- 通用参数---》所有字段类上都可以加
read_only?? ?表明该字段仅用于序列化输出,默认False
write_only?? ?表明该字段仅用于反序列化输入,默认False
-------------------------------------------------------
required?? ?表明该字段在反序列化时必须输入,默认True
default?? ? ? ? 反序列化时使用的默认值
allow_null?? ? 表明该字段是否允许传入None,默认False
validators?? ? 该字段使用的验证器:validators=[方法],方法对该字段做校验
error_messages?? ?包含错误编号与错误信息的字典
---------------
label?? ? ? ? 用于HTML展示API页面时,显示的字段名称
help_text?? ? 用于HTML展示API页面时,显示的字段帮助提示信息
?修改文件夹名
?修改项目名
?能顺利运行,按照如下图操作
?如果改了文件夹名,全局替换
1 修改字段,映射字段 publish_name表中不存在 publish_name = serializers.CharField(source='name') 2 修改字段,映射方法 # sb_name是表模型中一个方法 name = serializers.CharField(source='sb_name') 3 修改字段,跨表查询 book表中可以链表查询 publish=models.ForeignKey(to='Publish.name')
方案一:在表模型中写,在序列化类中映射 (可以使用source) # models.py def publish_detail(self): return {'name': self.publish.name, 'city': self.publish.city} # serializer.py publish_detail = serializers.DictField() # 前端看到 "publish_detail": { "name": "北京出版本是", "city": "北京" }
2.
方案二:在序列化类中写SerializerMethodField,必须配合一个方法 get_字段名,方法返回什么,前端就看到什么 # Serializer.py publish_detail = serializers.SerializerMethodField() def get_publish_detail(self, obj): return {'name': obj.publish.name, 'city': obj.publish.city} # 前端看到 "publish_detail": { "name": "北京出版本是", "city": "北京" }