DJANGO REST Framework - API调用仅返回20个实体中的20个实体

问题描述 投票:0回答:1

Django 1.11.3,python 3.6,coreapi 2.3.3

我从客户端执行对我的网站代码的API调用。

listProducts = self.amyClient.getProducts()称:results = self.client.action(schema, ["products", "list"])

在网站方面它执行queryset = Product.objects.all(),没有过滤器。

get_queryset方法len(queryset)之前return queryset给了我52个条目。

在客户端len(listProduct)是20.我添加了几个条目只是为了看看会发生什么 - 在API调用中返回的实体的数量发生了变化(因此它不是“连接到错误的数据库”问题),在客户端它是总是20.在API调用results['count']是52,len(results['results'])是20。

将查询集转换为列表(即queryset = list(Product.Objects.all()))并没有改变任何东西,我真的没有想到它,只是因为在API调用代码中它已经是正确的。必须在接收(客户端)端截断它。什么?谢谢。

我的看法:

class ProductList(generics.ListAPIView):
    permission_classes = (IsBotOrReadOnly,)

    """
    API endpoint that allows users to be viewed or edited.
    """

    serializer_class = ProductSerializer

    schema = AutoSchema(
        manual_fields=[
            coreapi.Field("productcode"),
        ]
    )


    def get_queryset(self):


        productcode = self.request.query_params.get('productcode', None)

        queryset = Product.active.all()

        if productcode is not None:
            queryset = list(Product.active.filter(productcode=productcode))
        else:
            queryset = Product.active.all()
        # prints 52
        print (len(queryset))
        return queryset
django rest api frameworks truncate
1个回答
0
投票

结果发送回客户端的输出被settings.py(服务器端)中的此设置截断为框架:

REST_FRAMEWORK = {
    .......
    'PAGE_SIZE': 20,
    .......
}

我真的很想知道为什么这是默认行为以及为什么默认值高达20 :-)。

感谢@Alasdair或指向我的分页文档。

© www.soinside.com 2019 - 2024. All rights reserved.