On Thu, Oct 1, 2009 at 4:38 PM, garyrob <gary...@mac.com> wrote:

>
> Hello,
>
> Suppose I have a model my_model, with several fields, one of which is
> x. Suppose it's based on an SQL table with the same name.
>
> In SQL, I can:
>
> select x, count(*)
> from my_model
> group by x
>
> In order to get the number of rows with each value of x.
>
> I'm not sure how to do that in django. The annotate method looks like
> the closest thing, but it seems to depend on having many-to-many table
> relationships.
>

I don't know how you got that impression?


>
> Is there some easy way to do this in django that I'm missing? Seems
> like there should be!
>
>
my_model.objects.values('x').annotate(Count('x'))

Perhaps the bit you were missing was the effect of values():
http://docs.djangoproject.com/en/dev/topics/db/aggregation/#values

Karen

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

  • group by garyrob
    • Re: group by Karen Tracey

Reply via email to