views:

112

answers:

2

When i have, say, a Datamodel named "Computer", and there are many Users for many Computers, I create a manytomany-relationship between "Computer" and "User". Now I want to select any Computer that is used by User 1. I tried this:

computers = Computer.objects.filter(users__contains=1)

But this does not seem to work since the __contains-operator acts as "like". What is the correct way?

+3  A: 

I believe it's as simple as:

computers = Computer.objects.filter(user__id=1)
Ned Batchelder
You don't really need the __id but it will work.
googletorp
+2  A: 

Just remove the __contains so you get.

computers = Computer.objects.filter(users=1)
googletorp