我查看了Django为查询运行的查询:
Model.objects.select_related('foo').filter(...)
注意到它看起来像:
{
u'time':u'0.056',
u'sql':u'SELECT
"model"."id",
"model"."foo_id",
<more model fields>
"foo"."id",
"foo"."bar_id",
<more foo fields>
FROM "model"
INNER JOIN "foo" ON ("model"."foo_id" = "foo"."id") WHERE <filter>
},
{
u'time':u'0.003',
u'sql':u'SELECT
"bar"."id",
<other fields>
FROM "bar" WHERE "bar"."id" = 6'
},
{
u'time':u'0.003',
u'sql':u'SELECT
"bar"."id",
<other fields>
FROM "bar" WHERE "bar"."id" = 6'
}
... many many more
表示它为相关的ForeignKeys生成加载其他查询。
我有两个问题:
我可以限制&#39;深度&#39;相关进入?
我能以某种方式告诉Django结果是稀疏的&#39;并首先创建一组ID,并仅查询那些?