视图集或序列化器中的DRF创建方法 [英] DRF create method in viewset or in serializer

查看:80
本文介绍了视图集或序列化器中的DRF创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义创建"和创建"之间有什么区别?DRF viewset中的方法或在序列化程序中自定义它我了解串行器负责反序列化数据,即在POST查询中呈现数据的方式;但是,我也可以在序列化程序的相关字段中创建对象.

What is the difference between customizing the "create" method in DRF viewset or customizing it in the serializer? I understand the serializer is responsible to deserialize the data, i.e. the way the data is presented in the POST query; however, I can also create objects in related fields in the serializer.

#views.py
def create(self, request):
    pass

#serializer.py
def create(self, validated_data):
    return Model.objects.create(**validated_data)

何时应该自定义视图/创建与序列化程序/创建?

When should I customize views/create vs. serializer/create?

推荐答案

从视图集创建方法

此方法处理视图中的 POST 请求逻辑,默认情况下执行以下操作:

create method from viewset

This method handles the POST request logic in the view, which by default does:

  • 使用请求中的有效负载数据来实例化序列化器
  • 在序列化程序上执行了 is_valid 方法
  • 通过在序列化程序上调用 .save()方法执行实际创建
  • 返回具有序列化数据和201状态的视图 Response
  • instantiate the serializer with whatever data comes as payload in the request
  • executed the is_valid method on the serializer
  • perform the actual create by calling .save() method on serializer
  • returns the view Response with serialized data and 201 status

您实际上不需要在视图集上覆盖 create 方法,如果您需要从视图本身将其发送给序列化程序,则可以覆盖 perform_create 默认情况下会执行 serializer.save().例如.如果您想从请求中发送用户,您可以执行以下操作:

You don't really need to override the create method on viewset, if is something that you need to send to the serializer from the view itself you can override perform_create which by default does serializer.save(). Eg. if you want to send the user from the request you can do:

    def perform_create(self, serializer):
        # here you will send `created_by` in the `validated_data` 
        serializer.save(created_by=self.request.user)

注意:在后台, save 方法将使用 validated_data

Note: Behind the scene save method will execute the create method on serializer with the validated_data

此方法仅使用 validated_data 创建实际的模型实例.如果需要创建相关对象,则可以重写此方法,如下所示:

This method just creates the actual model instance using the validated_data. You may override this method in case you need to create related objects, like the following:

   def create(self, validated_data):
        items_data = validated_data.pop('items')

        # similar to Parent.objects.create(**validated_data)
        parent = super().create(**validated_data)

        for item_data in items_data:
            Item.objects.create(parent=parent, **item_data)
        return parent

因此,这里您要发送的有效负载中包含有关 Parent 对象的数据,以及包含其表示形式的 items 列表,所以现在 create 方法还将创建Items并将它们与Parent实例链接.

So here you are sending a payload with data regarding the Parent object but also a list of items with their representation, so now the create method will create also the Items and link them with the Parent instance.

总结一下:

  • 在视图集中,create方法处理请求-响应流
  • 在序列化器中,create方法使用经过验证的数据来处理模型实例的创建.

这篇关于视图集或序列化器中的DRF创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆