简体   繁体   English

Django exclude(** kwargs)帮助

[英]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. 我有一个问题要问,我似乎无法找到解决问题的方法...基本上,我有一个名为Environment的模型,并且将所有这些模型传递给视图,并且存在一些特定的环境喜欢排除。 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: 现在,我知道queryset上有一个exclude函数,但是我似乎无法弄清楚如何将其用于多个选项...例如,我尝试了此操作但它没有用:

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. 但是唯一要排除的是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”])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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