标签:null test 名称 ima label meta als 主键 包含
如果我们想要使用序列化器对应的是Django的模型类,DRF为我们提供了ModelSerializer模型类序列化器来帮助我们快速创建一个Serializer类。
ModelSerializer与常规的Serializer相同,但提供了:
比如我们创建一个BookInfoSerializer
class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
fields = ‘__all__‘
我们可以在python manage.py shell中查看自动生成的BookInfoSerializer的具体实现
>>> from booktest.serializers import BookInfoSerializer
>>> serializer = BookInfoSerializer()
>>> serializer
BookInfoSerializer():
id = IntegerField(label=‘ID‘, read_only=True)
btitle = CharField(label=‘名称‘, max_length=20)
bpub_date = DateField(allow_null=True, label=‘发布日期‘, required=False)
bread = IntegerField(label=‘阅读量‘, max_value=2147483647, min_value=-2147483648, required=False)
bcomment = IntegerField(label=‘评论量‘, max_value=2147483647, min_value=-2147483648, required=False)
image = ImageField(allow_null=True, label=‘图片‘, max_length=100, required=False)
__all__
表名包含所有字段,也可以写明具体哪些字段,如class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
fields = (‘id‘, ‘btitle‘, ‘bpub_date‘)
class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
exclude = (‘image‘,)
class HeroInfoSerializer2(serializers.ModelSerializer):
class Meta:
model = HeroInfo
fields = ‘__all__‘
depth = 1
形成的序列化器如下:
HeroInfoSerializer():
id = IntegerField(label=‘ID‘, read_only=True)
hname = CharField(label=‘名称‘, max_length=20)
hgender = ChoiceField(choices=((0, ‘male‘), (1, ‘female‘)), label=‘性别‘, required=False, validators=[<django.core.valators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>])
hcomment = CharField(allow_null=True, label=‘描述信息‘, max_length=200, required=False)
hbook = NestedSerializer(read_only=True):
id = IntegerField(label=‘ID‘, read_only=True)
btitle = CharField(label=‘名称‘, max_length=20)
bpub_date = DateField(allow_null=True, label=‘发布日期‘, required=False)
bread = IntegerField(label=‘阅读量‘, max_value=2147483647, min_value=-2147483648, required=False)
bcomment = IntegerField(label=‘评论量‘, max_value=2147483647, min_value=-2147483648, required=False)
image = ImageField(allow_null=True, label=‘图片‘, max_length=100, required=False)
class HeroInfoSerializer(serializers.ModelSerializer):
hbook = BookInfoSerializer()
class Meta:
model = HeroInfo
fields = (‘id‘, ‘hname‘, ‘hgender‘, ‘hcomment‘, ‘hbook‘)
可以通过read_only_fields指明只读字段,即仅用于序列化输出的字段
class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
fields = (‘id‘, ‘btitle‘, ‘bpub_date‘, ‘bread‘, ‘bcomment‘)
read_only_fields = (‘id‘, ‘bread‘, ‘bcomment‘)
我们可以使用extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
fields = (‘id‘, ‘btitle‘, ‘bpub_date‘, ‘bread‘, ‘bcomment‘)
extra_kwargs = {
‘bread‘: {‘min_value‘: 0, ‘required‘: True},
‘bcomment‘: {‘min_value‘: 0, ‘required‘: True},
}
# BookInfoSerializer():
# id = IntegerField(label=‘ID‘, read_only=True)
# btitle = CharField(label=‘名称‘, max_length=20)
# bpub_date = DateField(allow_null=True, label=‘发布日期‘, required=False)
# bread = IntegerField(label=‘阅读量‘, max_value=2147483647, min_value=0, required=True)
# bcomment = IntegerField(label=‘评论量‘, max_value=2147483647, min_value=0, required=True)
标签:null test 名称 ima label meta als 主键 包含
原文地址:https://www.cnblogs.com/yanadoude/p/13167542.html