django 中多参数过滤器和链式过滤器的区别 [英] difference between filter with multiple arguments and chain filter in django

查看:39
本文介绍了django 中多参数过滤器和链式过滤器的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django 中多参数过滤器和链式过滤器有什么区别?

What is the difference between filter with multiple arguments and chain filter in django?

推荐答案

正如您在生成的 SQL 语句中所看到的,区别并不在于某些人可能怀疑的OR".WHERE 和 JOIN 就是这样放置的.

As you can see in the generated SQL statements the difference is not the "OR" as some may suspect. It is how the WHERE and JOIN is placed.

示例 1(相同的连接表):来自 https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

Example1 (same joined table): from https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

Blog.objects.filter(
       entry__headline__contains='Lennon', 
       entry__pub_date__year=2008)

这将为您提供具有一个条目的所有博客,其中包含(entry__headline__contains='Lennon') AND (entry__pub_date__year=2008),这正是您所期望的从这个查询.

This will give you all the Blogs that have one entry with both (entry__headline__contains='Lennon') AND (entry__pub_date__year=2008), which is what you would expect from this query.

结果:

Blog with {entry.headline: 'Life of Lennon', entry.pub_date: '2008'}

示例 2(链接)

Blog.objects.filter(
       entry__headline__contains='Lennon'
           ).filter(
       entry__pub_date__year=2008)

这将涵盖示例 1 中的所有结果,但会生成更多的结果.因为它首先使用 (entry__headline__contains='Lennon') 过滤所有博客,然后从结果过滤 (entry__pub_date__year=2008).

This will cover all the results from Example 1, but it will generate slightly more result. Because it first filters all the blogs with (entry__headline__contains='Lennon') and then from the result filters (entry__pub_date__year=2008).

不同之处在于它还会为您提供如下结果:

The difference is that it will also give you results like:

一个包含多个条目的博客

A single Blog with multiple entries

{entry.headline: '**Lennon**', entry.pub_date: 2000}, 
{entry.headline: 'Bill', entry.pub_date: **2008**}

当第一个过滤器被评估时,由于第一个条目(即使它有不匹配的其他条目),这本书被包含在内.当评估第二个过滤器时,由于第二个条目,该书被包括在内.

When the first filter was evaluated the book is included because of the first entry (even though it has other entries that don't match). When the second filter is evaluated the book is included because of the second entry.

一张表:但如果查询不涉及连接表,如 Yuji 和 DTing 的示例.结果是一样的.

One table: But if the query doesn't involve joined tables like the example from Yuji and DTing. The result is same.

这篇关于django 中多参数过滤器和链式过滤器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆