sort django queryset by latest instance of a subset of related model

后端 未结 2 2050
时光取名叫无心
时光取名叫无心 2021-01-20 19:56

I have an Order model and order_event model. Each order_event has a foreignkey to order. so from an order instance i can get: myorder.order_event_set. I want to

2条回答
  •  广开言路
    2021-01-20 20:33

    Ok, I think you can achieve this with the extra method, but not, as far as I can tell, with pure database-agnostic orm. This means you'll have to ensure your sql works for the db backend you're using, and to be wary of django aliasing the table names, but it will also mean you can do it all with one query.

    Something along these lines should work:

    latest_event_subquery = 'select max(event_datetime) ' \
                            'from appname_order_event ' \
                            'where appname_order_event.category="scheduling" and ' \
                                  'appname_order_event.event_id=appname_order.id'
    queryset = Order.objects.extra(select={'latest_event_date': latest_event_subquery}).order_by('latest_event_date')
    

    Does that make sense?

提交回复
热议问题