问题
Consider this query:
query = Novel.objects.< ...some filtering... >.annotate(
latest_chapter_id=Max("volume__chapter__id")
)
Actually what I need is to annotate each Novel
with its latest Chapter
object, so after this query, I have to execute another query to select actual objects by annotated IDs. IMO this is ugly. Is there a way to combine them into a single query?
回答1:
No, it's not possible to combine them into a single query.
You can read the following blog post to find two workarounds.
回答2:
Yes, it's possible.
To get a queryset containing all Chapters which are the last in their Novels, simply do:
from django.db.models.expressions import F
from django.db.models.aggregates import Max
Chapters.objects.annotate(last_chapter_pk=Max('novel__chapter__pk')
).filter(pk=F('last_chapter_pk'))
Tested on Django 1.7.
来源:https://stackoverflow.com/questions/12135174/in-django-is-there-a-way-to-directly-annotate-a-query-with-a-related-object-in