gpt4 book ai didi

python - 如何将 JSON 数组添加到 DRF 响应

转载 作者:行者123 更新时间:2023-12-01 06:24:49 24 4
gpt4 key购买 nike

我有一个返回以下内容的 DRF ListAPIView:

count: 35652
next: "https://platform/events/?format=json&page=2"
previous: null
results: (50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
__proto__: Object

我需要向 View 或序列化器添加什么才能返回它?

count: 35652
next: "https://platform/events/?format=json&page=2"
previous: null
results: (50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
filter_data: ['filter element 1', 'filter element 2']
__proto__: Object

最佳答案

由于您使用 ListAPIView,因此您可以重写 list() 方法,请记住,此方法需要返回一个 Response 对象(也在源代码中 take a look代码)(受到 this SO answer 的启发)。

   def list(self, request, *args, **kwargs):
response = super().list(request, args, kwargs)

# you can add the data that you need in the response
response.data['filter_data'] = ['filter element 1', 'filter element 2']

return response

此外,don't forget .data 包含已经序列化的数据。

关于python - 如何将 JSON 数组添加到 DRF 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com