Django:你能说出相关字段是否已被预取而没有取出它?

时间:2013-10-29 03:54:36

标签: python django

我想知道Django是否有办法判断一个相关字段,特别是一对多关系中的“很多”部分,是否已经通过prefetch_related()获取而没有实际获取它?

因此,举个例子,假设我有这些模型:

class Question(Model):
  """Class that represents a question."""

class Answer(Model):
  """Class the represents an answer to a question."""
  question = ForeignKey('Question', related_name='answers')

通常,要获得问题的答案数量,最有效的方法是执行以下操作(因为Django文档声明如果您只需要计数,count()会更有效率) :

# Note: "question" is an instance of class Question.
answer_count = question.answers.count()

然而,在某些情况下,答案可能是通过prefetch_related()调用(或某种方式,例如之前已经迭代完答案)获取的。所以在这样的情况下,这样做会更有效(因为我们会跳过额外的计数查询):

# Answers were fetched via prefetch_related()
answer_count = len(question.answers.all())

所以我真正想要的是:

if question.answers_have_been_prefetched:  # Does this exist?
  answer_count = len(question.answers.all())
else:
  answer_count = question.answers.count()

如果重要的话,我正在使用Django 1.4。提前谢谢。

修改:添加说明prefetch_related()不是获取答案的唯一方式。

1 个答案:

答案 0 :(得分:24)

是的,Django将预取结果存储在父模型实例的_prefetched_objects_cache属性中。

所以你可以这样做:

instance = Parent.objects.prefetch_related('children').all()[0]

try:
    instance._prefetched_objects_cache[instance.children.prefetch_cache_name]
    # Ok, it's pefetched
    child_count = len(instance.children.all())
except (AttributeError, KeyError):
    # Not prefetched
    child_count = instance.children.count()

请参阅django源干线中的relevant use或v1.4.9中的the equivalent