Django 使用 OR 条件进行查询

发布于 2024-09-25 10:28:35 字数 215 浏览 3 评论 0原文

在 Django 中,如何查询以获得两个不同的值,如下所示?

profile_setting = (pSetting.objects.get(module="my_mod", setting_value=1) or
                   pSetting.objects.get(module="my_mod", setting_value=0))

In Django, how do you query to get two different values, like in the following?

profile_setting = (pSetting.objects.get(module="my_mod", setting_value=1) or
                   pSetting.objects.get(module="my_mod", setting_value=0))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

聽兲甴掵 2024-10-02 10:28:35

查看 django 的 Q-class

profile_setting = pSetting.objects.get(Q(module="my_mod"),\
                  Q(setting_value=1)|Q(setting_value=0))

此外,为了提高您的编码风格,请查看一些编码指南,您最好将模型类命名为 PSetting

Checkout django's Q-class:

profile_setting = pSetting.objects.get(Q(module="my_mod"),\
                  Q(setting_value=1)|Q(setting_value=0))

Furthermore to improve your coding style, have a look at some coding guidelines, you should better name your model class PSetting.

东北女汉子 2024-10-02 10:28:35

您确定您只抓取一件物体吗?

如果你试图获取一堆对象的查询集,你需要做的就是将过滤器链接在一起:

profile_setting = pSetting.objects.filter(module="my_mod", setting_value__in=0)
                         .filter(module="my_mod", setting_value__in=1)

但是,由于除了setting_value之外的所有内容都是相同的,你可以简单地查找列表或元组:

profile_setting = pSetting.objects.filter(module="my_mod", setting_value__in=[0,1])

(alexdb上面的建议很好当且仅当您确定只会获得一个对象作为对查询的响应)

Are you sure that you are grabbing only one object?

If you are trying to grab a queryset of a bunch of objects, all you need to do is chain filters together:

profile_setting = pSetting.objects.filter(module="my_mod", setting_value__in=0)
                         .filter(module="my_mod", setting_value__in=1)

However, since everything but setting_value is the same, you can simply look for a list or tuple:

profile_setting = pSetting.objects.filter(module="my_mod", setting_value__in=[0,1])

(alexdb's suggestion above is fine if and only if you are sure that you'll be getting only one object as a response to your query)

孤君无依 2024-10-02 10:28:35

此任务有一个 Q() 对象 - 请看这里:使用 Q() 对象的复杂查询

例如:

profile_setting = pSetting.objects.get(
    Q(module="my_mod", setting_value=1) | Q(module="my_mod", setting_value=0)
)

There is a Q() object for this task - look here: Complex Queries with Q() object

In example:

profile_setting = pSetting.objects.get(
    Q(module="my_mod", setting_value=1) | Q(module="my_mod", setting_value=0)
)
风筝有风,海豚有海 2024-10-02 10:28:35
 profile_setting = pSetting.objects.get(module="my_mod",setting_value__in=[0,1])
 profile_setting = pSetting.objects.get(module="my_mod",setting_value__in=[0,1])
温暖的光 2024-10-02 10:28:35

对于这种类型的查询,请检查此文档 链接

,对于您的具体问题,您可以传递多个查询集对象(Q)
用于查询集使用 |对于 OR 条件和 & get() 和 filter() 函数中的 AND 条件。

For this type of query check this documentation link

and for your specific problem you can pass multiple queryset objects(Q)
for queryset use | for OR condition and & for AND condition in both get() and filter() functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文