Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对django queryset进行排序和切片_Python_Django - Fatal编程技术网

Python 对django queryset进行排序和切片

Python 对django queryset进行排序和切片,python,django,Python,Django,我试图进行一个查询,对对象进行排序,然后返回前100个 Orders.objects.order_by('date', 'time')[:100] 但是我得到了这个错误 Cannot reorder a query once a slice has been taken. 我将如何进行此查询 编辑: 上下文 Django-tables2出现此错误,它需要对查询调用order,但是您传递的是一个已经切片的列表。本文介绍了一种变通方法。也可以仅使用渲染部分,如中提供的示例所示: 对于模板: {#

我试图进行一个查询,对对象进行排序,然后返回前100个

Orders.objects.order_by('date', 'time')[:100]
但是我得到了这个错误

Cannot reorder a query once a slice has been taken.
我将如何进行此查询

编辑: 上下文


Django-tables2出现此错误,它需要对查询调用order,但是您传递的是一个已经切片的列表。本文介绍了一种变通方法。也可以仅使用渲染部分,如中提供的示例所示:

对于模板:

{# tutorial/templates/people.html #}
{% load render_table from django_tables2 %}
{% load static %}
<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" />
    </head>
    <body>
        {% render_table people %}
    </body>
</html>
{#tutorial/templates/people.html}
{%load render_Tables from django_Tables 2%}
{%load static%}
{%render_table people%}

查询和切片应该可以工作。你能把涉及的全部代码都展示出来吗?您使用的是管理器吗?我使用的是django-tables2,
OrderTable
的代码是什么?您确定在传递列表时不需要queryset吗?我不这么认为,它对.objects.all()很有效。添加了OrderTable的代码。我认为是django表给出了此错误,请检查问题。
from django.shortcuts import render

def people(request):
    return render(request, 'people.html', {'people': Person.objects.all()})
{# tutorial/templates/people.html #}
{% load render_table from django_tables2 %}
{% load static %}
<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" />
    </head>
    <body>
        {% render_table people %}
    </body>
</html>