English 中文(简体)
django:有多少问题造成?
原标题:django: how many queries does this cause?

Say I have the following models:

class Baz(models.Model):
    winning = models.CharField(max_length=100)

class Bar(models.Model):
    baz = models.ForeignKey(Baz)

class Foo(models.Model):
    bar = models.ForeignKey(Bar)

现在我有<代码>Foo,foo。 下面几行的问话有多少?

winning = foo.bar.baz.winning

是否每个外国钥匙<代码>?? 或者Django的智能是否足够了在这里仅作一问?

最佳回答

这将需要3个问题。

获取oo、酒吧和面包。

Use select_related() to get them at once.

Foo.objects.get(id=1).select_related( bar__baz__winning )
问题回答

可以迅速说明。 Install Django Toolbar。 然后检查提问表。 它将给你以各种定性信息。

Though I believe +1 query per FK call

这里是<代码>winning = foo.bar.baz.winning statement:(我不计Foo.objects. statemnt)

(0.000) SELECT "foo_bar"."id", "foo_bar"."baz_id" FROM "foo_bar" WHERE "foo_bar"."id" = 1 ; args=(1,)
(0.000) SELECT "foo_baz"."id", "foo_baz"."winning" FROM "foo_baz" WHERE "foo_baz"."id" = 1 ; args=(1,)

And here is my tip: add logging for django.db.backends in your settings. https://docs.djangoproject.com/en/dev/topics/logging/#django-db-backends

您将看到你在座各位的所有询问。 或安装Django debug工具bar作为Francis建议你。





相关问题
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 ]="...

热门标签