Thanks for the response. If only it were that easy :). In my
categories table, each category should be unique. What I am trying to
count is the m2m ref table.

Example.
category_id | name
1 | programming
2 | politics
3 | games

blog_id | category_id

1 | 2  <-- I am trying to reference this table and count how many
times each unique tag name is used.
1 | 3
2 | 1
2 | 2
3 | 3
3 | 2

 so if category_id 2 name is 'politics' it would know that it occurs 3
times. make sense?  The easy way would be to create a counter field in
the categories table and count each time that the category is being
used but that is redundant and require extra work to save the category
count each time I add a new entry.  The answer is already there I just
need to figure out how to retrieve that answer. :)


On Jan 25, 9:00 am, Thomas Guettler <[EMAIL PROTECTED]> wrote:
> Hi,
>
> There are two ways of counting:
>
> # Bad way:
> len(MyModel.objects.filter(...))
>
> This will fetch all rows .... (SELECT * FROM ... WHERE ...;)
>
> # Good way:
> MyModel.objects.filter(...).count()
>
> This will do the couting at the database
> SELECT COUNT(*) FROM ... WHERE ...;
>
> If you know how to get all categories by using filter(), just
> put .count() at the end and you get the result.
>
> HTH,
>  Thomas Güttler
>
> Am Freitag, 25. Januar 2008 08:47 schrieb Chris:
>
> > hello again. thanks for all the help that everyone has offered me
> > here.
>
> > I have a generic django application called categories and it serves to
> > provide categories for various apps suchas a weblog or an articles
> > application. So a weblog and article app both have a M2M relation to
> > the catergories app. what I am wanting to do is count how many
> > occurrences of a category are found for a particular application. So
> > if my weblog had a category called politics, it would count how many
> > weblogs have a relation with that. Is this possible with out putting a
> > category_counter field in? I notice that there is a .count() method
> > but not really sure how to apply it to what I am attempting to do.
>
> > -model looks like:
> > name
> > slug
> > content_type
>
> > -my call:
> > test =
> > Category.objects.filter(content_type__app_label__exact='weblog')
> > how would I count the number of occurrences of a category called
> > politics?
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to