Deniz Dogan wrote:
> Hey.
> 
> I have a model Course and then I have django.contrib.auth.models.User.
> Users and courses are related through M2M, so a user can register for
> many courses and a course can register multiple users. Now, given a
> potentially very long list of users, I want to retrieve all the
> courses for which all of those users have registered. Currently I have
> implemented it in this manner:
> 
> courses = Course.objects.all()
> for user in users:
>   courses = courses.filter(users=user)
> courses = courses.distinct()
> 
> As you might expect, I get the "maximum 32 tables in a join" error
> when having a very long list of users. How would I go about doing what
> I want here?

http://www.djangoproject.com/documentation/db-api/#in

user_ids = [u.id for u in users]
# or user_ids = User.objects.values_list("id", flat=true)
courses = Course.objects.filter(users__id__in=user_ids)

I've used 'in' with a many hundreds ids.  Have no idea how performant it 
is compared to other solutions.


-- 
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___________________________________________________________________________
You've got fun!  Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

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