带多个参数的过滤器和django中的链过滤器之间的差异 [英] difference between filter with multiple arguments and chain filter in django

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

问题描述

解决方案

正如你在生成的SQL语句的区别不在于某些可能会怀疑的OR。这是WHERE和JOIN的放置方式。



示例1(相同的连接表):从 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 ='列侬')AND(entry__pub_date__year = 2008),这是你期望从这个查询。



结果:

 预订{entry.headline:'Life的Lennon',entry.pub_date:'2008'} 

示例2(链接)

  Blog.objects.filter(
entry__headline__contains ='列侬'
).filter(
entry__pub_date__year = 2008 )

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



区别在于它还会给你以下结果:



使用

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

一个表: / strong>但是如果查询不涉及连接表,例如Yuji和DTing的示例。结果相同。


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

解决方案

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.

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)

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.

Result:

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

Example 2 (chained)

Blog.objects.filter(
       entry__headline__contains='Lennon'
           ).filter(
       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:

Book with

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

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天全站免登陆