Django:“QuerySet .__ nonzero__”和“QuerySet.exists”之间的区别? [英] Django: Any difference between `QuerySet.__nonzero__` and `QuerySet.exists`?

查看:137
本文介绍了Django:“QuerySet .__ nonzero__”和“QuerySet.exists”之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 QuerySet 类有两种不同的方法,似乎有相同的用途(除非我错误): .__ nonzero __ .exists 。 (是的,我知道 .__非零__ bool 使用。)

I see that the QuerySet class has 2 different methods that seem to serve the same purpose (unless I'm mistaken): .__nonzero__ and .exists. (Yes, I know that .__nonzero__ is used by bool.)

我的问题:如果两个方法只是检查查询集中是否有任何对象,那么为什么这两个方法有不同的实现?

My question: Why do the 2 methods have different implementation if they both just check whether there's any object in the queryset?

Django文档说明了 QuerySet .__ nonzero __

The Django documentation says about QuerySet.__nonzero__:


注意:如果您想要做确定是否至少有
一个结果存在,并且不需要实际的对象。更多
使用exists()(见下文)。

Note: Don't use this if all you want to do is determine if at least one result exists, and don't need the actual objects. It's more efficient to use exists() (see below).

(我没有找到任何有见地的东西在下面。)

(I didn't find anything insightful "below".)

为什么 QuerySet .__ nonzero __ 有一个非高效的实现?是否尝试实现与 .exists 不同的东西? Django开发人员不做的原因是什么?code> __ nonzero__ =存在?

Why does QuerySet.__nonzero__ have a non-efficient implementation? Is it trying to achieve something different than .exists? What is the reason that the Django developers don't do __nonzero__ = exists?

推荐答案


为什么QuerySet。非零有一个非有效的实现?它是试图实现一些不同于.exists的东西吗? Django开发人员不执行非零 =存在的原因是什么?

Why does QuerySet.nonzero have a non-efficient implementation? Is it trying to achieve something different than .exists? What is the reason that the Django developers don't do nonzero = exists?

我认为这是因为只在某些情况下才有效。

I think it's because exists is only efficient under certain circumstances.

想像一下这个常见的情况,如果 __ nonzero __ 有高效的实现。

Imagine this common scenario if __nonzero__ had the "efficient" implementation.

foos = Foo.objects.filter(bar='baz')

if foos:   # nonzero() calls exists() which causes extra query
           # even though the QS is already going to be evaluated
           # which in my projects is a common pattern. 
   print "Yay for foos!"
   for foo in foos:
       print foo

__nonzero __ 还会评估查询并将结果存储在缓存中。这意味着所有结果都存储在内存中。

__nonzero__ also evaluates the query and stores results in cache. That means all results are stored in memory.

存在仅关心1行,甚至不会实例化一个django对象,甚至将其结果存储在缓存中。

exists only cares about 1 row, and doesn't even instantiate a django object or even store its result in cache.

如果您只是检查是否存在某些东西,并且不需要数据以任何方式。

It seems useful if you are only checking if something exists, and don't need the data in any way.


为什么开发者不会使 __非零__ = = 存在

由于存在假定您不关心结果。如果 __ nonzero __ 调用存在,我们将没有结果。如果存在调用 __ nonzero __ ,我们将收集结果(可能很多)只是为了检查行是否存在

Because exists assumes you don't care about the results. If __nonzero__ called exists, we'd have no results. If exists called __nonzero__, we'd collect results (potentially a lot) just to check if a row exists or not.

示例:

bool( Foo.objects.filter(user=user) )  
# calls __nonzero__, evaluates, converts all fields to python objects 
# and stores in queryset._result_cache


Foo.objects.filter(user=user).exists()
# stores nothing, and query only returns one row.
# less data from DB and less python overhead generating django model instances.

这篇关于Django:“QuerySet .__ nonzero__”和“QuerySet.exists”之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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