On Mon, Aug 18, 2014 at 1:06 PM,  <[email protected]> wrote:
> Is it possible to annotate two different values to one QuerySet?
>
> Example queryset:
>
> Item.objects.annotate(score=Count('votes'))

Yes, simply pass it as an additional argument to annotate(). An
example from one of my projects:

    shows = TVSeries.objects.all().order_by('name').annotate(
            num_episodes=Count('tvepisode'),
            num_episodes_with_media=Count('tvepisode__media_id'),
            num_aired_episodes=Count('tvepisode__airdate'),
            num_seasons=Max('tvepisode__season'))

So it counts the number of episodes, how many have media, how many
have aired, and what the maximum season is for each series.

>
> I would like every item in the above QuerySet to contain the following
> field:
>
> current_user_vote = Vote.objects.filter(user=request.user).filter(item=item)

As an aside, this can probably be written as:

  current_user_vote = item.vote_set.filter(user=request.user)

>
> I tried to use annotate() two times, however it filtered out everything
> except items current user voted on.

Just use it once.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1%2BM%2BbuFpGrz04HmT8n130z7%2BtauCnHBBoZL6diKM-2FDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to