English 中文(简体)
有没有一种方法可以抵制在使用Django的orm时只检查id存在的不必要的联接?
原标题:Is there a way to resist unnecessary joins that are only checking id existence when using Django s orm?

例如。如果我有一个模特,她有一个母字段,这是一把外键。。我想到了以下几点:

p = Person.object.get(id=1)
if p.mother_id:
    print "I have a mother!"

在上面的示例中,我们发出了一个查询。我用_id字段而不是mother.id欺骗Django不获取母亲。但如果我过滤所有没有母亲的人:

Person.objects.filter(mother=None)
Person.objects.filter(mother__id=None)
Person.objects.filter(mother__isnull=True)
Person.objects.filter(mother__id__isnull=True)

所有这些都不必要地加入到相关的表中。。我不能引用_id列,因为它们不是字段。。因此以下任一项失败:

Person.objects.filter(mother_id__isnull=True)
Person.objects.filter(mother_id=None)

有没有一种方法可以让我构建一个querySet,在不引发联接的情况下检查外键列中是否存在值?

提前谢谢。

Edit (answered): Credit goes to Bernd, who commented on Daniel s answer, but it turns out that this workaround does splendidly for returning people without mothers, without issuing the unnecessary join:

Person.objects.exclude(mother__isnull=False)

编辑(更多详细信息):

我还应该提到的是,我发现这种行为实际上似乎只有在FK关系为null时才会引起注意。这很奇怪,但却是真的。

问题回答

我对此感到惊讶-我认为Person.object.filter(mother=None)在没有额外联接的情况下也能工作-但经过检查,结果证明你是对的。事实上,这已被记录为Django的票证跟踪器中的一个错误

不幸的是,记录它的人——以及(重新)编写大部分Django查询代码的人——不再积极地为Django做出贡献,所以我不知道什么时候才能真正修复它。你可以试试那张票上的一个补丁,看看它们是否有帮助。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签