Annotate on a foreign key returns foreign keys. How can I get the objects themselves from the query? In the following example 'the_user' is a foreign key in the "Vote" model:
Vote.objects.values('the_user').annotate(vote_count=Count('the_user')).order_by('-vote_count')
It would return
[{'the_user': 4, 'vote_count': 12} , {'the_user': 6, 'vote_count': 2}]
But I need the user objects themselves.. Not the ids
values()
does exactly that - returns values, use usual queryset
Vote.objects.annotate(vote_count=Count('the_user')).order_by('-vote_count')
Then each object in that queryset will have vote_count
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With