Python Django Rest Framework UnorderedObjectListWarning [英] Python Django Rest Framework UnorderedObjectListWarning

查看:105
本文介绍了Python Django Rest Framework UnorderedObjectListWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Django 1.10.4升级到1.11.1,突然我在运行测试时收到大量这些消息:

I upgraded from Django 1.10.4 to 1.11.1 and all of a sudden I'm getting a ton of these messages when I run my tests:

lib/python3.5/site-packages/rest_framework/pagination.py:208:
UnorderedObjectListWarning: 
Pagination may yield inconsistent results with an unordered object_list: 
<QuerySet [<Group: Requester>]>
paginator = self.django_paginator_class(queryset, page_size)

我已经追溯到Django分页模块:
https:// github.com/django/django/blob/master/django/core/paginator.py#L100

I've traced that back to the Django Pagination module: https://github.com/django/django/blob/master/django/core/paginator.py#L100

这似乎与我的查询集代码有关:

It seems to be related to my queryset code:

return get_user_model().objects.filter(id=self.request.user.id)

如何找到此警告的更多详细信息?似乎我需要在每个过滤器的末尾添加 order_by(id),但是我似乎找不到要添加order_by的代码(因为警告不会返回堆栈跟踪,因此它会在我的测试运行期间随机发生)。

How can I find more details on this warning? It seems to be that I need to add a order_by(id) on the end of every filter, but I can't seem to find which code needs the order_by added (because the warning doesn't return a stack trace and so it happens randomly during my test run).

谢谢!

编辑:

所以通过使用@KlausD。详细信息提示,我查看了一个导致此错误的测试:

So by using @KlausD. verbosity tip, I looked at a test causing this error:

response = self.client.get('/ api / orders /')

这将转到 OrderViewSet ,但是get_queryset中的所有内容均不会导致它,并且序列化程序类导致它。我还有其他使用相同代码获取/ api / orders的测试,而这些测试不会导致它.... get_queryset之后DRF会做什么?

This goes to OrderViewSet but none of the things in get_queryset cause it and nothing in serializer class causes it. I have other tests that use the same code to get /api/orders and those don't cause it.... What does DRF do after get_queryset?

https://github.com/encode/django-rest -framework / blob / master / rest_framework / pagination.py#L166

如果我在分页中添加了追溯功能,那么我会得到很多与django rest框架,但没有任何指向我的查询触发顺序警告的内容。

If I put a traceback into pagination then I get a whole bunch of stuff related to django rest framework but nothing that points back to which of my queries is triggering the order warning.

推荐答案

因此,为了解决此问题我必须找到所有全部偏移量过滤器 limit 子句,并在其中添加 order_by 子句。我通过添加默认顺序来解决一些问题:

So in order to fix this I had to find all of the all, offset, filter, and limit clauses and add a order_by clause to them. Some I fixed by adding a default ordering:

class Meta:
   ordering = ['-id']

在Django Rest Framework的ViewSets(app / apiviews.py)中,我必须更新所有 get_queryset 方法似乎无法正常工作。

In the ViewSets for Django Rest Framework (app/apiviews.py) I had to update all of the get_queryset methods as adding a default ordering didn't seem to work.

希望这对其他人有帮助。 :)

Hope this helps someone else. :)

这篇关于Python Django Rest Framework UnorderedObjectListWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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