如何告诉Django ORM反转查询结果的顺序? [英] How can I tell the Django ORM to reverse the order of query results?

查看:146
本文介绍了如何告诉Django ORM反转查询结果的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻求理解针对Django模型的查询的过程中,我一直在尝试使用以下查询来获取最后3个添加的有效Avatar模型:

In my quest to understand queries against Django models, I've been trying to get the last 3 added valid Avatar models with a query like:

newUserAv = Avatar.objects.filter(valid=True).order_by("date")[:3]

但是,这反而给了我按日期顺序添加的前三个头像。我敢肯定这很简单,但是在Django文档中却很难找到它:如何选择 last 三个化身对象而不是前三个?

However, this instead gives me the first three avatars added ordered by date. I'm sure this is simple, but I've had trouble finding it in the Django docs: how do I select the last three avatar objects instead of the first three?

推荐答案

在字段名称前加上连字符。

Put a hyphen before the field name.

.order_by('-date')

这篇关于如何告诉Django ORM反转查询结果的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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