When annotating something to a model in Django one would write something like this:
Post.objects.all().annotate(total_yes_votes=Count('vote', filter=Q(vote__vote_choice='yes')))
What if I want to annotate on a single object and not all() the objects:
Post.objects.get(id=id).annotate(total_yes_votes=Count('vote', filter=Q(vote__vote_choice='yes')))
By the way, it doesn't work.
Isn't it more efficient to annotate a single object rather than all()?
将调用.get()
移到注释之后
Post.objects.annotate(total_yes_votes=Count('vote', filter=Q(vote__vote_choice='yes'))).get(id=id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.