views:

52

answers:

2

Hey guys/gals!

I had a question for you, something that I can't seem to find the solution for... Basically, I have a model called Environment, and I am passing all of them to a view, and there are particular environments that I would like to exclude. Now, I know there is a exclude function on a queryset, but I can't seem to figure out how to use it for multiple options... For example, I tried this but it didn't work:

kwargs = {"name": "env1", "name": "env2"}
envs = Environment.objects.exclude( kwards )

But the only thing that it will exclude is the last "name" value in the list of kwargs. I understand why it does that now, but I still can't seem to exclude multiple objects with one command. Any help is much appreciated!

Shawn

+3  A: 

The way to do this would be:

Enviroment.objects.exclude(name="env1").exclude(name="env2")

or

Enviroment.objects.exclude(Q(name="env1") | Q(name="env2"))
Alex Gaynor
Hmmm alright then. The multiple exclude solution is the one I went with before asking the question, but I was hoping for a more elegant solution :) Thanks anyways!
shawnjan
+1  A: 

Enviroment.objects.exclude(name__in=["env1","env2"])

vh5
Interesting, I should go back and try this, maybe tomorrow at work!
shawnjan