Django的REST框架:直接显示在GenericView结果列表 [英] Django REST Framework: Directly display on results list in GenericView

查看:411
本文介绍了Django的REST框架:直接显示在GenericView结果列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 djangorestframework 来管理REST API的Andr​​oid移动应用程序连接到我的Django的Web应用程序。我有我需要从通过​​REST API的Web应用程序检索对象的名单,迄今通用 ListCreateAPIView 工作正常。

I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.

不过,在REST API的回报是不完全列表/阵列的本身的,但含有JSON对象元数据和实际结果列表。下面是该输出的一个例子:

However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "foo":"bar"
        }
    ]
}

问题是,我的移动应用程序的REST客户希望一个JSON列表/数组,而不是上面的JSON对象。有没有办法让我的普通视图中删除计数接下来 previous 元数据和只输出结果列表本身?我需要像下面的输出:

The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:

[
    {"foo":"bar"},
    {"foo":"something"},
    {"foo":"another"}
]

哦,我不知道这将是有益的,但​​我用改造作为REST客户对我的Andr​​oid应用程序,这应该连接到我的web应用程序的REST API。

Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.

推荐答案

被查询集分页程序生成的这个对象封装了数组。如果禁用分页你会得到数组。要禁用分页,设置 paginate_by

This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:

class PaginatedListView(ListAPIView):
    queryset = ExampleModel.objects.all()
    serializer_class = ExampleModelSerializer
    paginate_by = None

这篇关于Django的REST框架:直接显示在GenericView结果列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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