首页 > 其他分享 >drf高级五ModelSerializer

drf高级五ModelSerializer

时间:2023-02-22 14:06:00浏览次数:32  
标签:ModelSerializer field many create 高级 instance 序列化 data drf

不同于Serializer,可以帮助我们完成。

  • 它将根据模型自动生成一组字段。
  • 它将根据模型上的参数自动生成序列化程序的验证程序。
  • 它包括 create() 和 update()简单默认实现。
from rest_framework import serializers

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        # exclude = ('created', ) 
        # read_only_fields = ('email', ) 指定只读字段
        # extra_kwargs = {'created': {'write_only': True}}

Meta属性

指定字段—fields

  • __all__指模型下所有字段
  • 元组或者列表指定字段

指定字段—exclude

和fields正好相反,指不需要一些字段。字段比较多它可能比较好用。

注意:无论是feilds or exclude被过滤的的字段都不能序列化或者反序列化。

指定只读字段

指定字段只做序列化,不做反序列化。功能等同字段参数read_only=True。默认就被设置为只读的不需要。

附加关键词

还可以通过使用extra_kwargs选项快捷地在字段上指定任意附加的关键字参数。在read_only_fields这种情况下,你不需要在序列化器上式的声明该字段。

Serializer

其实,在日常使用过程中。ModelSerializer比较常用,很多继承自Serializer被使用。

to_representation

重写这个选项作为序列化读取。使用ModeSerializer时为了适应反序列化。如:关联外键and choices类型and 额外附加字段等。

class CommentSerializer(BaseSerializer):
    def to_representation(self, data):

        ret = super().to_representation(data)
        if ret:
            # 逻辑
            ret['a'] = 'hello'
            pass

        return ret

create

通过验证数据将被保存

    def create(self, validated_data):
        """
        We have a bit of extra checking around this in order to provide
        descriptive messages when something goes wrong, but this method is
        essentially just:

            return ExampleModel.objects.create(**validated_data)

        If there are many to many fields present on the instance then they
        cannot be set until the model is instantiated, in which case the
        implementation is like so:

            example_relationship = validated_data.pop('example_relationship')
            instance = ExampleModel.objects.create(**validated_data)
            instance.example_relationship = example_relationship
            return instance

        The default implementation also does not handle nested relationships.
        If you want to support writable nested relationships you'll need
        to write an explicit `.create()` method.
        """
        raise_errors_on_nested_writes('create', self, validated_data)

        ModelClass = self.Meta.model

        # Remove many-to-many relationships from validated_data.
        # They are not valid arguments to the default `.create()` method,
        # as they require that the instance has already been saved.
        info = model_meta.get_field_info(ModelClass)
        many_to_many = {}
        for field_name, relation_info in info.relations.items():
            if relation_info.to_many and (field_name in validated_data):
                many_to_many[field_name] = validated_data.pop(field_name)

        try:
            instance = ModelClass._default_manager.create(**validated_data)
        except TypeError:
            tb = traceback.format_exc()
            msg = (
                'Got a `TypeError` when calling `%s.%s.create()`. '
                'This may be because you have a writable field on the '
                'serializer class that is not a valid argument to '
                '`%s.%s.create()`. You may need to make the field '
                'read-only, or override the %s.create() method to handle '
                'this correctly.\nOriginal exception was:\n %s' %
                (
                    ModelClass.__name__,
                    ModelClass._default_manager.name,
                    ModelClass.__name__,
                    ModelClass._default_manager.name,
                    self.__class__.__name__,
                    tb
                )
            )
            raise TypeError(msg)

        # Save many-to-many relationships after the instance is created.
        if many_to_many:
            for field_name, value in many_to_many.items():
                field = getattr(instance, field_name)
                field.set(value)

        return instance

update

数据将被更新

    def update(self, instance, validated_data):
        raise_errors_on_nested_writes('update', self, validated_data)
        info = model_meta.get_field_info(instance)

        # Simply set each attribute on the instance, and then save it.
        # Note that unlike `.create()` we don't need to treat many-to-many
        # relationships as being a special case. During updates we already
        # have an instance pk for the relationships to be associated with.
        m2m_fields = []
        for attr, value in validated_data.items():
            if attr in info.relations and info.relations[attr].to_many:
                m2m_fields.append((attr, value))
            else:
                setattr(instance, attr, value)

        instance.save()

        # Note that many-to-many fields are set after updating instance.
        # Setting m2m fields triggers signals which could potentially change
        # updated instance and we do not want it to collide with .update()
        for attr, value in m2m_fields:
            field = getattr(instance, attr)
            field.set(value)

        return instance

createupdate将共用验证器。所以在使用序列化器时应该考虑更加全面。序列化器默认是保存和更新是根据序列化后得到的字段。如果只是操作部分字段使用Serializer(partial=True)默认为False。

标签:ModelSerializer,field,many,create,高级,instance,序列化,data,drf
From: https://blog.51cto.com/u_14967494/6067586

相关文章

  • ansible 高级属性
    目录ansible高级属性本地执行任务委托任务暂停滚动执行只执行一次设置环境变量交互式提示tags标签为task打tag执行一个设定tag一次指定多个tag执行排除指定tag的task查看p......
  • 28-DRF框架-搭建环境开发restful接口
    #将程序中的一个数据结构类型转换为其他格式(字典、JSON、XML等),例如将Django中的模型类对象装换为JSON字符串,这个转换过程我们称为序列化#将其他格式(字典、JSON、XML等)转......
  • Vue高级
    目录Vue高级1.ref属性2.props配置项-父传子自定义属性3.mixin混入--了解4.插件--了解5.scoped样式6.插槽1.匿名插槽2.具名插槽7.Router8.localStorage、sessionStora......
  • DRF view
    DRFviewViewDRF中的view分成三个等级,最基本的APIView,到GenericAPIView,再到GenericViewSet.Django用“视图”这个概念封装处理用户请求并返回响应的逻辑。视图是一......
  • drf反序列化时出错
    -问题Postman传递json,drf反序列化时接受不到值,一直提示None-json数据点击查看代码{"number":"1","warehouse":"1","members":"1","handle......
  • go 高级编程中一些注意点
    数组、字符串和切片三者是密切相关的数据结构,因底层都是相同的结构。go中除了闭包函数以引用的方式对外部变量访问之外,其它赋值和函数传参数都是以传值的方式处理。数......
  • 微信接口开发之高级篇系列【微信权限封装类WechatAuth】
    ThinkPHP框架目录结构:<?php/***CreatedbyPhpStorm.*User:Tinywan*Date:2016/9/11*Time:9:55*/namespaceOrg\Util;classWechatAuth{/*消息类型常量*......
  • 高级程序员解决问题的思维模式
    「NLP理解层次」将对一件事的理解,由低到高分为6个不同的层次,即维度。低维角度无法解决的问题,站在更高的维度理解,就会称为一个非常简单的问题,我愿称之为「降维打击」。接下来......
  • 第一章、nodejs高级
    目录一、nodejs基础1、认识nodejs2、nodejs的特性3、使用nodejs需要了解多少javascript4、浏览器环境vsnode环境5、开发环境搭建二、模块、包、commonjs1、为什么要有模块......
  • 14 DRF返回值的自定义
    django中的返回值可以分为:异常返回值正常返回值1.Djanog中常见的异常返回值有如下序列化校验错误{"age":["Avalidintegerisrequired."]......