django 查询集中的两个或多个 __in 过滤器
我有这个查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这其中的哪一部分令人困惑? 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
如果您尝试进行多键查找,这就是它们的工作方式。
或者,问题是您的“in”子句有一长串特定值?
如果您的列表很长,您可能需要分段构建查询。
以上未经测试。而且,它可能效率低下。
更好的是这样的。
这将一次执行许多非常高效的 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
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.
The above is untested. Also, it's probably inefficient.
What's better is something like this.
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.
逗号分隔查询。
示例:
有时,回答问题比争论问题是否有效更好。
Comma-delimit the query.
Example:
Sometimes it's better to answer the question rather than to bicker about it being a valid question.