[Answered ]-Order by count of ManyToMany field in django


The ticket specifically mentions MySQL. I’m using PostgreSQL and use annotations all the time with no noticeable slowness. However, I’ve never specifically checked the generated SQL to see if it’s doing the same thing.

If you are using MySQL, it would seem you potentially have the following choices (albeit, none of them particular awesome):

  1. Switch over to PostgreSQL. If my anecdotal evidence and the lack of complaints regarding PostgreSQL in the ticket are any indication, it might not be a problem then.

  2. Live with it for now, upgrade to 1.4 when it’s released.

  3. Run off of trunk to get the fix now (not generally a good idea) or try to patch your current installation.

  4. Use raw() as @jknupp suggests.

4 is probably the easiest and best approach for the moment. It’s not really a problem unless you switch database engines at some point in the future, and you can always just make a note to yourself in the code to do it the right way when Django is updated. I typically do something like the following in similar scenarios:

"""Django has a bug in version 1.3 (see ticket:
https://code.djangoproject.com/ticket/17144), resulting in unnecessary fields
being included in the GROUP BY clause and subsequently very slow queries. The raw
query below can be replaced with the commented line once the issue has been
# projects.annotate(votes_count=Count('votes')).order_by('votes_count')


To get around this, you can use the raw() SQL query. It’s not the prettiest way to do it, but it will work. See the documentation here

Leave a comment