如何使用“AND” 在 Django 过滤器中?

发布于 2024-07-18 02:48:24 字数 324 浏览 9 评论 0 原文

如何创建“AND”过滤器来检索 Django 中的对象? 例如,我想检索在单个字段中包含两个单词组合的行。

例如,当我在 mysql 数据库上运行以下 SQL 查询时,它完全执行此操作:

SELECT * FROM myapp_question
WHERE ((question LIKE '%software%') AND (question LIKE '%java%'))

How do you finish this in Django usingfilters?

How do I create an "AND" filter to retrieve objects in Django? e.g I would like to retrieve a row which has a combination of two words in a single field.

For example the following SQL query does exactly that when I run it on mysql database:

SELECT * FROM myapp_question
WHERE ((question LIKE '%software%') AND (question LIKE '%java%'))

How do you accomplish this in Django using filters?

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

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

发布评论

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

评论(4

不离久伴 2024-07-25 02:48:24

为了彻底起见,我们只提一下 Q 对象方法:

from django.db.models import Q
criterion1 = Q(question__contains="software")
criterion2 = Q(question__contains="java")
q = Question.objects.filter(criterion1 & criterion2)

请注意,这里的其他答案更简单,更适合您的用例,但如果有人遇到类似但稍微复杂的问题(例如需要“不”或“或”)看到这一点,很高兴在这里有参考。

For thoroughness sake, let's just mention the Q object method:

from django.db.models import Q
criterion1 = Q(question__contains="software")
criterion2 = Q(question__contains="java")
q = Question.objects.filter(criterion1 & criterion2)

Note the other answers here are simpler and better adapted for your use case, but if anyone with a similar but slightly more complex problem (such as needing "not" or "or") sees this, it's good to have the reference right here.

回首观望 2024-07-25 02:48:24

更新:这个答案将不再有效,并给出语法错误关键字参数重复

mymodel.objects.filter(first_name__icontains="Foo", first_name__icontains="Bar")

更新:自从我写这个答案并完成以来已经很长时间了一些 django,但我确信目前最好的方法是使用 Q 对象方法,如 David Berger 所示:如何使用并且在 Django 过滤器中?

(update: this answer will not work anymore and give the syntax error keyword argument repeated)

mymodel.objects.filter(first_name__icontains="Foo", first_name__icontains="Bar")

update: Long time since I wrote this answer and done some django, but I am sure to this days the best approach is to use the Q object method like David Berger shows here: How do I use AND in a Django filter?

猫卆 2024-07-25 02:48:24

您可以在 Django 中链接过滤器表达式:

q = Question.objects.filter(question__contains='software').filter(question__contains='java')

您可以在 Django 文档中找到更多信息,网址为“链接过滤器”。

You can chain filter expressions in Django:

q = Question.objects.filter(question__contains='software').filter(question__contains='java')

You can find more info in the Django docs at "Chaining Filters".

嘿哥们儿 2024-07-25 02:48:24

您可以将 ANDfilter() 使用 &Q()& 如下所示:

# "store/views.py"

from .models import Question
from django.db.models import Q
from django.http import HttpResponse

def test(request):

    # With "&"
                                                              # ↓ Here
    qs = Question.objects.filter(question__contains="software") & \ 
         Question.objects.filter(question__contains="java")
    print(qs)

    # With "Q()" and "&"
                               # ↓ Here                         # ↓ Here
    qs = Question.objects.filter(Q(question__contains="software") & 
                                 Q(question__contains="java"))
    print(qs)                  # ↑ Here

    return HttpResponse("Test")

You can use AND with filter() using & or Q() and & as shown below:

# "store/views.py"

from .models import Question
from django.db.models import Q
from django.http import HttpResponse

def test(request):

    # With "&"
                                                              # ↓ Here
    qs = Question.objects.filter(question__contains="software") & \ 
         Question.objects.filter(question__contains="java")
    print(qs)

    # With "Q()" and "&"
                               # ↓ Here                         # ↓ Here
    qs = Question.objects.filter(Q(question__contains="software") & 
                                 Q(question__contains="java"))
    print(qs)                  # ↑ Here

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