2017-03-13 35 views
-1

全部。我的Queryset结果出现了一些小问题,我想通过改变方式来改变它。移位查询结果

menus = ShopMenu.objects.filter(shop=shop_id) 

他是返回结果我:

1 --- 
2 --- 
3 --- 
4 --- 

但是,这不是真的是我需要的。我想收到

4 ---- 
1 --- 
2 --- 
3 --- 

唧唧我能得到这一结果?

+0

叶氏。 itry做trhat –

回答

0

你可以得到聚合查询中的最后一个ID。

from django.db.models import Max 
last_id = ShopMenu.objects.aggregate(last_id=Max('id'))['last_id'] 

然后由该领域的其他字段添加到使用conditional expression您的查询和订单:

from django.db.models import Case, When, Value, BooleanField 
menus = ShopMenu.objects.annotate(
    show_before=Case(
     When(id=last_id, then=Value(True)), 
     default=Value(False), 
     output_field=BooleanField() 
    ) 
).order_by('show_before', 'id')