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

模型类序列化器ModelSerializer

程序员文章站 2022-04-10 21:54:12
定义比如我们创建一个BookInfoSerializerclass BookInfoSerializer(serializers.ModelSerializer): """图书数据序列化器""" class Meta: model = BookInfo fields = '__all__'model... ......

定义

比如我们创建一个bookinfoserializer

class bookinfoserializer(serializers.modelserializer):
    """图书数据序列化器"""
    class meta:
        model = bookinfo
        fields = '__all__'
  • model指明参照哪个模型类
  • fields指明为模型类的哪些字段生成

我们可以在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)

2.指定字段

1)使用字段来明确字段__all__表名包含所有字段,也可以写明具体哪些字段,如

class bookinfoserializer(serializers.modelserializer):
    """图书数据序列化器"""
    class meta:
        model = bookinfo
        fields = ('id', 'btitle', 'bpub_date')

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

class bookinfoserializer(serializers.modelserializer):
    """图书数据序列化器"""
    class meta:
        model = bookinfo
        exclude = ('image',)

3)默认modelserializer使用主键作为关联字段,但是我们可以使用depth来简单的生成嵌套表示,depth应该是整数,表明嵌套的层级数量。

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)

4)显示指明字段,如:

class heroinfoserializer(serializers.modelserializer):
    hbook = bookinfoserializer()

    class meta:
        model = heroinfo
        fields = ('id', 'hname', 'hgender', 'hcomment', 'hbook')

5)指明只读字段

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

class bookinfoserializer(serializers.modelserializer):
    """图书数据序列化器"""
    class meta:
        model = bookinfo
        fields = ('id', 'btitle', 'bpub_date', 'bread', 'bcomment')
        read_only_fields = ('id', 'bread', 'bcomment')

3.添加额外参数

我们可以使用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)