>> You cannot. Django does not do aggregates like GROUP BY yet. You will >> have to use manual sql to get those values. > > Thanks for your reply. > But how to use sql together with Django so that I can use advantages > of paginator?
You can inject SQL into your query with the .extra() call (I start to sound like a broken record, given how frequently I turn to using extra() as a solution :) http://www.djangoproject.com/documentation/db-api/#extra-select-none-where-none-params-none-tables-none This allows you to do something like (divining somewhat from your models) blogs_with_counts = Blogs.objects.extra(select={ 'tally': """ (SELECT Count(*) FROM mimi_blogs mb2 WHERE mb2.subject = mimi_blogs.subject )""" }) This will add a "tally" attribute to your resulting model which There's been some work on adding aggregates to Django...I have some working-ish code that did what I needed as posted at http://groups.google.com/group/django-users/browse_thread/thread/fc58fbe409a6f098/941bc6cc541d8746 It may or may not work for you, but it was something I hacked together to solve my particular need. Since then, some of these ideas and other aggregate-function ideas have been rolled into ticket #3566 http://code.djangoproject.com/ticket/3566 However, I'm not sure how far that ticket has progressed towards a working solution yet. IIUC, it's waiting on the refactoring of the queryset which may/should make implementing aggregates considerably less messy & hackish. -tim --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---