django 查询集中的两个或多个 __in 过滤器

发布于 2024-10-10 08:48:02 字数 235 浏览 3 评论 0原文

我有这个查询

query = 'select * from products where (productnr, supplier_id) in (%s)' % product_list

,其中 Product_list 看起来像这样 ((OB520, 3),(RH402, 20)...)

我如何使用 queryset 和 __in 过滤器在 Django 中执行此操作

I have this query

query = 'select * from products where (productnr, supplier_id) in (%s)' % product_list

where product_list looks like this ((OB520, 3),(RH402, 20)...)

How do I go about doing this in Django using queryset and the __in filter

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

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

发布评论

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

评论(2

牵强ㄟ 2024-10-17 08:48:02

这其中的哪一部分令人困惑? http://docs.djangoproject.com/en/1.2/ref/ models/querysets/#in 看起来很清楚。

从问题中还不能完全清楚问题是什么。

您想问如何使用多部分密钥吗?如果是这样,您将对简单的 __in 感到不满意。

如果您尝试查找由两部分组成的密钥的“OR”,则必须创建更复杂的条件。

从这里开始: http://docs .djangoproject.com/en/1.2/topics/db/queries/#complex-lookups-with-q-objects

from django.db.models import Q
product_list.filter(
Q(productnr='OB520', supplier_id=3) | Q(productnr='RH402', supplier_id=20))

如果您尝试进行多键查找,这就是它们的工作方式。

或者,问题是您的“in”子句有一长串特定值?

如果您的列表很长,您可能需要分段构建查询。

q_obj= Q()
for p, s in some_list_of_pairs;
   q_obj |= Q(productnr=p, supplier_id=s )
product_list.filter(q_obj)

以上未经测试。而且,它可能效率低下。

更好的是这样的。

def values_iter( some_list_of_pairs ):
    for p, s in some_list_of_pairs
        yield product_list.get(productnr=p, supplier_id=s) 

这将一次执行许多非常高效的 SQL 查找。这可能比构建复杂的多键 IN 子句执行得更快。

What part of this is confusing? http://docs.djangoproject.com/en/1.2/ref/models/querysets/#in It seems very clear.

It's not perfectly clear from the question what the problem is.

Are you asking how to use a multi-part key? If so, you're going to be unhappy with simple __in.

If you're trying to look for an "OR" of a two-part key, you have to create a more complex condition.

Start here: http://docs.djangoproject.com/en/1.2/topics/db/queries/#complex-lookups-with-q-objects

from django.db.models import Q
product_list.filter(
Q(productnr='OB520', supplier_id=3) | Q(productnr='RH402', supplier_id=20))

If you're trying to do multi-key lookups, this is they way it has to work.

Or, is the problem that your "in" clause has a long list of specific values?

If you have a long list, you might want to build the query in pieces.

q_obj= Q()
for p, s in some_list_of_pairs;
   q_obj |= Q(productnr=p, supplier_id=s )
product_list.filter(q_obj)

The above is untested. Also, it's probably inefficient.

What's better is something like this.

def values_iter( some_list_of_pairs ):
    for p, s in some_list_of_pairs
        yield product_list.get(productnr=p, supplier_id=s) 

That will do a number of very efficient SQL lookups one at a time. That may execute faster than building a complex multi-key IN clause.

静谧 2024-10-17 08:48:02

逗号分隔查询。

示例:

?groups__in=1,2

有时,回答问题比争论问题是否有效更好。

Comma-delimit the query.

Example:

?groups__in=1,2

Sometimes it's better to answer the question rather than to bicker about it being a valid question.

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