简体   繁体   中英

Django exclude(**kwargs) help

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

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"))

Enviroment.objects.exclude(name__in = [ “ENV1”, “ENV2”])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM