我有以下模型(部分)模型定义:
class Album(models.Model):
name = models.CharField(max_length=50,
null=False,
blank=True)
parent = models.ForeignKey("self",
null = True,
default = None,
blank = True,
help_text="Points to a parent album",
related_name="children")
当我运行manage.py debugsqlquery
并检查生成的查询时,我得到了一个不应该存在的连接:
>>> a_qs = Album.objects.filter(parent=None)
>>> for a in a_qs:
... print a
...
结果查询是:
SELECT "photos_album"."id",
"photos_album"."name",
"photos_album"."parent_id",
"photos_album"."owner_id",
"photos_album"."order",
"photos_album"."system",
"photos_album"."created",
"photos_album"."last_modified"
FROM "photos_album"
LEFT OUTER JOIN "photos_album" T2 ON ("photos_album"."parent_id" = T2."id")
WHERE T2."id" IS NULL
ORDER BY "photos_album"."order" ASC, "photos_album"."name" ASC [0.42ms]
我试图摆脱LEFT OUTER JOIN
我尝试过parent_id=None
的 ,parent__isnull=True
和parent_id__isnull=True
所有生成连接。
如何在不创建联接的情况下执行查询?