ModelSerializer与常规的Serializer相同,但提供了:
class BookInfoSerializer(serialzier.ModelSerializer):
class Meta:
model = BooksInfo # 指明参照哪个模型类
fields = '__all__' # 指明为模型类的哪些字段生成
实例:
from books.models import BooksInfo
serializer = BookInfoModelSerializer()
print(serializer)
'''
BookInfoModelSerializer():
id = IntegerField(label='ID', read_only=True)
title = CharField(label='名称', max_length=20)
pub_date = DateField(label='发布日期')
read = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
comment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
is_delete = BooleanField(label='逻辑删除', required=False)
'''
通过fields
来明确序列化/反序列化字段:
__all__
代表所有字段:class BookInfoSerializer(serialzier.ModelSerializer):
class Meta:
model = BooksInfo # 指明参照哪个模型类
fields = '__all__' # 指明为模型类的哪些字段生成
class BookInfoSerializer(serialzier.ModelSerializer):
class Meta:
model = BooksInfo # 指明参照哪个模型类
fields = ('id', 'title', 'pub_date') # 指明为模型类的哪些字段生成
exclude
字段可以明确排除掉哪些字段:class BookInfoSerializer(serialzier.ModelSerializer):
class Meta:
model = BooksInfo # 指明参照哪个模型类
exclude = ('is_delete',)
depth
来简单的生成嵌套表示,depth
应该是整数,表明嵌套的层级数量:class HeroInfoModelSerializer(serializers.ModelSerializer):
class Meta:
model = HeroInfo
fields = '__all__'
depth = 1
'''
HeroInfoModelSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(label='名称', max_length=20)
gender = ChoiceField(choices=((0, 'male'), (1, 'female')), label='性别', required=False, validators=[<django.core.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>])
desc = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
is_delete = BooleanField(label='逻辑删除', required=False)
hero_book = NestedSerializer(read_only=True):
id = IntegerField(label='ID', read_only=True)
title = CharField(label='名称', max_length=20)
pub_date = DateField(label='发布日期')
read = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
comment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
is_delete = BooleanField(label='逻辑删除', required=False)
'''
read_only_fields
指明只读字段,即仅用于序列化输出的字段class BookInfoModelSerializer(serializers.ModelSerializer):
class Meta:
model = BooksInfo
fields = '__all__'
read_only_fields = ('id', 'read', 'comment')
设置的只读字段还是能传入并save成功,只是不会修改数据库
通过使用extra_kwargs
参数可以为ModelSerializer
添加或修改原有的选项参数
class BookInfoModelSerializer(serializers.ModelSerializer):
class Meta:
model = BooksInfo
fields = '__all__'
extra_kwargs = {
'read': {'min_value': 0},
'comment': {'min_value': 0}
}
'''
BookInfoModelSerializer():
id = IntegerField(label='ID', read_only=True)
title = CharField(label='名称', max_length=20)
pub_date = DateField(label='发布日期')
read = IntegerField(label='阅读量', max_value=2147483647, min_value=0, required=False)
comment = IntegerField(label='评论量', max_value=2147483647, min_value=0, required=False)
is_delete = BooleanField(label='逻辑删除', required=False)
'''
基于Nginx+Supervisord+uWSGI+Django1.11.1+Python3.6.5构建