问题:Django order_by查询集,升序和降序

如何按日期降序对django中的查询集进行排序?

Reserved.objects.all().filter(client=client_id).order_by('check_in')

我只想从所有的check_in日期按降序过滤。

How can I order by descending my query set in django by date?

Reserved.objects.all().filter(client=client_id).order_by('check_in')

I just want to filter from descending all the Reserved by check_in date.


回答 0

Reserved.objects.filter(client=client_id).order_by('-check_in')

注意-之前check_in

Django文档

Reserved.objects.filter(client=client_id).order_by('-check_in')

Notice the - before check_in.

Django Documentation


回答 1

Reserved.objects.filter(client=client_id).order_by('-check_in')

“ check_in”前面的连字符“-”表示降序。隐含升序。

我们不必在filter()之前添加all()。那仍然可以工作,但是只需要从根QuerySet中获取所有对象时就需要添加all()。

此处的更多信息:https : //docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters

Reserved.objects.filter(client=client_id).order_by('-check_in')

A hyphen “-” in front of “check_in” indicates descending order. Ascending order is implied.

We don’t have to add an all() before filter(). That would still work, but you only need to add all() when you want all objects from the root QuerySet.

More on this here: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters


回答 2

您还可以使用以下说明:

Reserved.objects.filter(client=client_id).order_by('check_in').reverse()

You can also use the following instruction:

Reserved.objects.filter(client=client_id).order_by('check_in').reverse()

回答 3

对于升序:

Reserved.objects.filter(client=client_id).order_by('check_in')

对于降序:

1.  Reserved.objects.filter(client=client_id).order_by('-check_in')

要么

2.  Reserved.objects.filter(client=client_id).order_by('check_in')[::-1]

for ascending order:

Reserved.objects.filter(client=client_id).order_by('check_in')

for descending order:

1.  Reserved.objects.filter(client=client_id).order_by('-check_in')

or

2.  Reserved.objects.filter(client=client_id).order_by('check_in')[::-1]

回答 4

它可以删除.all()

Reserved.objects.filter(client=client_id).order_by('-check_in')

It works removing .all():

Reserved.objects.filter(client=client_id).order_by('-check_in')

回答 5

添加-将以降序排列。您还可以通过向模型的元数据添加默认顺序来进行设置。这意味着当您执行查询时,只需执行MyModel.objects.all(),它将以正确的顺序显示。

class MyModel(models.Model):

    check_in = models.DateField()

    class Meta:
        ordering = ('-check_in',)

Adding the – will order it in descending order. You can also set this by adding a default ordering to the meta of your model. This will mean that when you do a query you just do MyModel.objects.all() and it will come out in the correct order.

class MyModel(models.Model):

    check_in = models.DateField()

    class Meta:
        ordering = ('-check_in',)

回答 6

  1. 升序

    Reserved.objects.all().filter(client=client_id).order_by('check_in')
  2. 降序

    Reserved.objects.all().filter(client=client_id).order_by('-check_in')

- (连字符)用于指示降序。

  1. Ascending order

    Reserved.objects.all().filter(client=client_id).order_by('check_in')
    
  2. Descending order

    Reserved.objects.all().filter(client=client_id).order_by('-check_in')
    

- (hyphen) is used to indicate descending order here.


回答 7

这对我有用。

latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]

This is working for me.

latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]

回答 8

67

Reserved.objects.filter(client = client_id).order_by(’-check_in’)

“-”表示降序,对于升序,仅提供类属性

67

Reserved.objects.filter(client=client_id).order_by(‘-check_in’)

‘-‘ is indicates Descending order and for Ascending order just give class attribute


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。