作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个返回以下内容的 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/
我是一名优秀的程序员,十分优秀!