简体   繁体   English

Django Rest Framework:如何在GET请求的响应中添加自定义字段?

[英]Django Rest Framework : How to add a custom field to the response of the GET request?

I'm new to DRF and have just started building an API. 我是DRF的新手,刚刚开始构建API。

I've a model called Shop . 我有一个名为Shop的模型。 And I've two user different user types : Customer and Supplier . 我有两个用户不同的用户类型: 客户供应商

  1. I want to add a custom field distance to the response of the GET request /shops/id/ , which represents the distance between the Customer that is submitted the request and the corresponding shop. 我想在GET请求/ shops / id /的响应中添加一个自定义字段距离 ,该距离代表提交请求的客户与相应商店之间的距离。
  2. I think I cannot use SerializerMethodField since the value of the method is not only depend on the object itself. 我认为我不能使用SerializerMethodField,因为方法的值不仅取决于对象本身。
  3. I do not want to add this custom field for all GET requests, instead, I need to add it, when the user that is submitted the request is a Customer . 我不想为所有GET请求添加此自定义字段,而是在提交请求的用户是Customer时添加它。

Considering constraints above, how should I add the custom field to the response of the request? 考虑到以上约束,我应该如何将自定义字段添加到请求的响应中? What's the best way to do this ? 最好的方法是什么?

You can define a distance SerializerMethodField , and there access the current user location using serializer's context . 您可以定义一个distance SerializerMethodField ,然后使用serializer的context访问当前用户位置。 Then compute distance using current user location and shop's location. 然后使用当前用户位置和商店位置计算距离。

class ShopSerializer(serializers.ModelSerializer):

    distance = serializers.SerializerMethodField()

    class Meta:
        model = Shop
        fields = (.., 'distance')

    def get_distance(self, obj):
        current_user = self.context['request'].user # access current user    
        user_location = current_user.location

        distance = <compute distance using obj.location and user_location>
        return distance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在GET时在Django rest框架的Response()中添加自定义数据? - Add custom data in Response() of django rest framework while GET? Django Rest Framework - 如何在 ModelSerializer 中添加自定义字段 - Django Rest Framework - How to add custom field in ModelSerializer 如何在Django REST Framework中向GET请求添加搜索参数? - How to add search parameters to GET request in Django REST Framework? 在Django Rest框架中转储自定义过滤字段作为响应 - Dump A Custom Filtered Field as Response in Django Rest Framework 在 django rest 框架中返回对成功 POST 请求的自定义响应 - Return custom response to successful POST request in django rest framework 自定义字段 Django Rest Framework - Custom field Django Rest Framework Django Rest框架中的GET请求无法与响应JSON对象中的过滤器一起使用 - GET request in Django Rest framework is not working with filter in the response JSON object 如何在 Django Rest 框架中为单个发布请求发送两个响应? - How to send two response for a single post request in Django Rest Framework? 蟒蛇。 Django Rest框架。 如何获得自定义的Json响应? - Python. Django Rest Framework. How to get a custom Json response? 如何在python Django Rest Framework中的序列化器中获取其余请求? - How to get rest of the request in serializer in python django rest framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM