On Tue, Dec 6, 2011 at 9:35 PM, Reinout van Rees <rein...@vanrees.org> wrote: > On 06-12-11 21:11, wgis wrote: >> >> I get >> (Carrots, Flavor,2.0) >> >> I want to list all the contexts in the "Carrot template", withou >> having to search and merge the ones missing. >> So if the result was >> >> (Carrots, Flavour, 2.0) >> (Carrots, Smell, 0.0) >> (Carrots, Usability, 0.0) >> (Carrots, Size, 0.0) >> or >> (Carrots, Flavour, 2.0) >> (Carrots, Smell, null) >> (Carrots, Usability, null) >> (Carrots, Size, null) > > > Ah! Now I get your point. You also want the "empty" results for which > there's no SQL data. Sorry, but I don't see a way in which you can do that > with an SQL query (and so also not with a Django query). > > In case you want all contexts, you'll have to query for those specifically. > And afterwards grab the results belonging to that context. So you won't > escape a for loop and some manual work, I'm afraid. >
Isn't this the secret sauce he is looking for? >>> from django.db.models import Q, Sum >>> qs = VoteContext.objects.filter(Q(vote__thing=carrot) | >>> Q(vote__isnull=True)) >>> qs = qs.annotate(vote_value=Sum('vote__vote')) >>> for vote_ctxt in qs: print vote_ctxt.name, vote_ctxt.vote_value ... Flavour 2.0 Smell None Usability None Size None Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.