Monday, August 18, 2014

Re: How to annotate many values in one queryset

On Mon, Aug 18, 2014 at 6:33 PM, Collin Anderson <cmawebsite@gmail.com> wrote:
>> 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'))
>
> Woah. Is that new?
>

No, I think it has always been available like that since aggregation was added.

annotate(*args, **kwargs)

Annotates each object in the QuerySet with the provided list of
aggregate values (averages, sums, etc) that have been computed over
the objects that are related to the objects in the QuerySet. Each
argument to annotate() is an annotation that will be added to each
object in the QuerySet that is returned.


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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/CAFHbX1LvvLfr1EmGE%2B8jP0xpfW5m46tHpxcPzEhnH0dhBvEE7A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment