为什么我遵循了order_by()函数的常规用法,为什么仍收到UnorderedObjectList警告?

时间:2018-12-15 19:07:41

标签: django python-3.x pagination django-queryset

我有一个通用的列表视图,可以从其中使用两个单独模型(Message和safeTransaction)中的查询集进行分页。

django终端输出在进入我的收件箱视图时发出此警告:

/usr/local/lib/python3.6/dist-packages/django/views/generic/list.py:88:UnorderedObjectListWarning:分页可能会产生与无序列表object_list不一致的结果:QuerySet。   allow_empty_first_page = allow_empty_first_page,**歪曲)

这是View和get_context_data方法:

### Inbox list class
class InboxListView(ListView):

#This view lets the user view all the messages created in a list

model = Message
template_name = "myInbox/inbox.html"
paginate_by = 3 # paginating twice here 
#so the page gets paginated in a convenient way for my template.

def get_context_data(self, **kwargs):
    message_list = Message.objects.filter(recipient=self.request.user)
    .order_by('-date')
    safeTransaction_list = SafeTransaction.objects
        .filter(trans_recipient=self.request.user)
        .order_by('-date') 
    queryset = list(chain(message_list, safeTransaction_list))
    #paginator setup start
    paginator = Paginator(queryset,3)
    page = self.request.GET.get('page')
    safe_T_list = paginator.get_page(page) #iterable list for template
    #paginator setup done
    context = super(InboxListView, self).get_context_data(**kwargs)
    context.update({
        'now': timezone.now(),
        'message_list':safe_T_list,
    })
    return context

我在此方法中所做的就是将过滤后的查询集链接到一个列表中,然后将其作为一个查询集进行分页。问题在于,即使在执行此操作之前,我过滤的邮件和SafeTransactions仍被视为无序列表。 这让我感到困惑,因为django网站上order_by()的典型用法类似于我所做的。

总而言之,我想使用从不同对象库构建的查询集进行分页,但是出于我不明白的原因,我的过滤对象被认为是无序的。

堆栈跟踪:

Starting development server at http://127.0.0.1:4200/
Quit the server with CONTROL-C.
[17/Dec/2018 07:52:52] "GET / HTTP/1.1" 200 1490
[17/Dec/2018 07:52:52] "GET /static/css/mystyle.css HTTP/1.1" 200 1555
[17/Dec/2018 07:52:52] "GET /static/css/w3pro.css HTTP/1.1" 200 15672
[17/Dec/2018 07:52:52] "GET /static/css/w3.css HTTP/1.1" 200 23293
[17/Dec/2018 07:52:52] "GET /static/favicon.ico HTTP/1.1" 200 8192
/usr/local/lib/python3.6/dist-packages/django/views/generic/list.py:88: UnorderedObjectListWarning: Pagination may yield inconsistent results with             an unordered object_list: <class 'account.models.Message'> QuerySet.
  allow_empty_first_page=allow_empty_first_page, **kwargs)
[17/Dec/2018 07:52:58] "GET /inbox HTTP/1.1" 200 3134
[17/Dec/2018 07:52:58] "GET /static/css/mystyle.css HTTP/1.1" 200 1555

0 个答案:

没有答案