我想知道 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()
不是获取答案的唯一方式。
是的,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 源代码主干中的相关用法或 v1.4.9 中的等效用