Fantastic, thanks! This is 1.1 only though correct?
Cheers :) Darren On Jun 5, 11:39 am, Russell Keith-Magee <freakboy3...@gmail.com> wrote: > On Fri, Jun 5, 2009 at 11:24 PM, Darren<blogposts.dar...@gmail.com> wrote: > > > Hey, > > > Would anyone be able to to help me convert this SQL to Django code? > > > SELECT A.a, COUNT(B.b) > > FROM A, B > > WHERE A.id = B.a_id > > GROUP BY A.id > > ORDER BY A.id; > > It's impossible to give you a canonical answer without a complete > Django model, but the query you have written would be rendered using > something like: > A.objects.values('a').annotate(Count(b__b)).order_by('id') > > As a point of clarification - if you actually only want a count of B > objects, not a count of unique values of B.b, then you could use: > > A.objects.values('a').annotate(Count(b)).order_by('id') > > The values() clause is also potentially optional. The purpose of that > clause is to reduce the output set so that it contains only the field > 'a' and the count. If you are happy with returning full A instances, > annotated with the count, you would use: > > A.objects.annotate(Count(b)).order_by('id') > > Yours, > Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---