欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DRF框架-模型类序列化器ModelSerializer

程序员文章站 2022-07-12 11:11:30
...

如果我们想要使用序列化器对应的是Django的模型类,DRF为我们提供了ModelSerializer模型类序列化器来帮助我们快速创建一个Serializer类。

一、创建序列化器

class BookSerializer(serializers.ModelSerializer):
    # 图书数据序列化器
    # model 指明该序列化器处理的数据字段从模型类BookInfo参考生成
    # fields 指明该序列化器包含模型类中的哪些字段,'__all__'指明包含所有字段
   class Meta:
       model = BookInfo
       fields = '__all__'

class HeroSerializer(serializers.ModelSerializer):
    # 图书数据序列化器
    # model 指明该序列化器处理的数据字段从模型类HeroInfo参考生成
    # fields 指明该序列化器包含模型类中的哪些字段,'__all__'指明包含所有字段
    class Meta:
        model = HeroInfo
        fields = '__all__'
 1)使用fields来明确字段,__all__表名包含所有字段,也可以写明具体哪些字段 :
 fields = ('id', 'btitle', 'bpub_date')

2) 使用exclude可以明确排除掉哪些字段

 exclude = ('image',)
3) 默认ModelSerializer使用主键作为关联字段
>>> from booktest.serializers import HeroSerializer
>>> serializer = HeroSerializer()
>>> serializer
HeroSerializer():
    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.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>])
    hcomment = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    hbook = PrimaryKeyRelatedField(label='图书', queryset=BookInfo.objects.all())

但是我们可以使用depth来简单的生成嵌套表示,depth应该是整数,表明嵌套的层级数量。如:

class HeroInfoSerializer2(serializers.ModelSerializer):
    class Meta:
        model = HeroInfo
        fields = '__all__'
        depth = 1

测试结果如下:

>>> from booktest.serializers import HeroInfoSerializer2
>>> serializer = HeroInfoSerializer2()
>>> serializer
HeroInfoSerializer2():
    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.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>])
    hcomment = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    hbook = NestedSerializer(read_only=True):
        id = IntegerField(label='ID', read_only=True)
        btitle = CharField(label='名称', max_length=20)
        bpub_date = DateField(label='发布日期')
        bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
        bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
        is_delete = BooleanField(label='逻辑删除', required=False)
        image = ImageField(allow_null=True, label='图片', max_length=100, required=False)

4) 显示指明字段,如:

class HeroSerializer3(serializers.ModelSerializer):
    hbook = BookSerializer()
    class Meta:
        model = HeroInfo
        fields = ('id','hname','hgender','hcomment','hbook')

测试结果:

>>> from booktest.serializers import HeroSerializer3
>>> from booktest.models import HeroInfo
>>> serializer = HeroSerializer3()
>>> serializer
HeroSerializer3():
    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.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>])
    hcomment = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
    hbook = BookSerializer():
        id = IntegerField(label='ID', read_only=True)
        btitle = CharField(label='名称', max_length=20)
        bpub_date = DateField(label='发布日期')
        bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
        bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
        is_delete = BooleanField(label='逻辑删除', required=False)
        image = ImageField(allow_null=True, label='图片', max_length=100, required=False)

5) 指明只读字段

可以通过read_only_fields指明只读字段,即仅用于序列化输出的字段

class BookSerializer2(serializers.ModelSerializer):
    class Meta:
        model = BookInfo
        fields = ('id', 'btitle', 'bpub_date', 'bread', 'bcomment')
        read_only_fields = ('id', 'bread', 'bcomment')

二、查看序列化器

>>> from booktest.serializers import BookSerializer,HeroSerializer
>>> serializer = BookSerializer()
>>> serializer
BookSerializer():
    id = IntegerField(label='ID', read_only=True)
    btitle = CharField(label='名称', max_length=20)
    bpub_date = DateField(label='发布日期')
    bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
    bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    image = ImageField(allow_null=True, label='图片', max_length=100, required=False)
>>> serializer = HeroSerializer()
>>> serializer
HeroSerializer():
    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.validators.MinValueValidator object>, 		<django.core.validators.MaxValueValidator object>])
    hcomment = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    hbook = PrimaryKeyRelatedField(label='图书', queryset=BookInfo.objects.all())

三、使用序列化器

ModelSerializer与常规的Serializer相同,但提供了:

  • 基于模型类自动生成一系列字段

  • 基于模型类自动为Serializer生成validators,比如unique_together

  • 包含默认的create()和update()的实现

    如果创建序列化器对象的时候,没有传递instance实例,则调用save()方法的时候,create()被调用,相反,如果传递了instance实例,则调用save()方法的时候,update()被调用。

    下面以图书数据的序列化器演示序列化器的使用

    如下:添加数据库

    >>> from booktest.serializers import BookSerializer
    >>> from booktest.models import BookInfo
    >>> from datetime import date
    >>> data = {'btitle':'武则天传奇','bpub_date':date(1990,3,5)}
    >>> serializer = BookSerializer(data=data)   # 没有传递instance实例,新建数据
    >>> serializer.is_valid()
    True
    >>> serializer.save()
    如下:更新数据库
>>> from booktest.models import BookInfo
>>> from booktest.serializers import BookSerializer
>>> from datetime import date
>>> book = BookInfo.objects.get(id=9)   # 实例:instance
>>> book
<BookInfo: 三国演义(第二版)>
>>> data={'btitle':'三国演义<改编版>','bpub_date':date(1992,5,8)}
>>> serializer = BookSerializer(book,data=data)   # 传进了instance参数,表示更新数据库
>>> serializer.is_valid()
True
>>> serializer.save()
<BookInfo: 三国演义<改编版>>

四、添加额外参数

class BookSerializer(serializers.ModelSerializer):
    # 图书数据序列化器
    # model 指明该序列化器处理的数据字段从模型类BookInfo参考生成
    # fields 指明该序列化器包含模型类中的哪些字段,'__all__'指明包含所有字段
   class Meta:
       model = BookInfo
       fields = '__all__'
# 使用extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
class BookSerializer3(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}
        }    
       

测试:

# 做对比:
>>> from booktest.serializers import BookSerializer,BookSerializer3
>>> ser = BookSerializer()
>>> ser
BookSerializer():
    id = IntegerField(label='ID', read_only=True)
    btitle = CharField(label='名称', max_length=20)
    bpub_date = DateField(label='发布日期')
    bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
    bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    image = ImageField(allow_null=True, label='图片', max_length=100, required=False)
    
# 测试添加参数的序列化器
>>> ser2 = BookSerializer3()
>>> ser2
BookSerializer3():
    id = IntegerField(label='ID', read_only=True)
    btitle = CharField(label='名称', max_length=20)
    bpub_date = DateField(label='发布日期')
    bread = IntegerField(label='阅读量', max_value=2147483647, min_value=0, required=True)
    bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=0, required=True)


相关标签: DRF框架