简体   繁体   English

Django ORM - get()with order_by()

[英]Django ORM - get() with order_by()

I want a mix of get() and order_by() like this 我想要像这样混合使用get()和order_by()

Model.objects.get(some=condition).order_by('name')

I want a single result (not filter) and ordered by name at the same time. 我想要一个结果(不是过滤器)并同时按名称排序。

怎么样

Model.objects.filter(some=condition).order_by('name').first()

get() wont take order_by() as get only returns one single object and you cannot order by one one row. get()不会使用order_by()因为get只返回一个对象而你不能按一行排序。

and for order by you need multiple rows for the ordering. 并且对于订单,您需要多行进行排序。 use filter('your where clause').order_by('asc/desc') 使用filter('你的where子句')。order_by('asc / desc')

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM