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