Django queryset to return first of each item in foreign key based on date

前端 未结 1 1697
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 17:22

need to get a queryset with the first book (by a date field) for each author (related to by foreign key) ...is there a Django ORM way to do this (without custom SQL preferre

相关标签:
1条回答
  • 2021-01-05 17:46

    If you use PostgreSQL or another DB backend with support for DISTINCT ON there is a nice solution:

    Books.objects.order_by('author', '-date').distinct('author')
    

    Otherwise I don't know a solution with only one query. But you can try this:

    from django.db.models import Q, Max
    import operator
    
    books = Book.objects.values('author_id').annotate(max_date=Max('date'))
    filters = reduce(operator.or_, [(Q(author_id=b['author_id']) &
        Q(date=b['max_date'])) for b in books])
    queryset = Books.objects.filter(filters)
    

    With the combination of .values() and .annotate() we group by the author and annotate the latest date of all books from that author. But the result is a dict and not a queryset. Then we build a SQL statement like WHERE author_id=X1 AND date=Y1 OR author_id=X2 AND date=Y2.... Now the second query is easy.

    0 讨论(0)
提交回复
热议问题