鉴于以下模型,Django在第一次访问它们之后缓存相关对象吗?Django在访问后是否缓存相关的ForeignKey和ManyToManyField字段?
class Post(models.Model):
authors = models.ManyToManyField(User)
category = models.ForeignKey(Category)
例如:
post = Post.objects.get(id=1)
# as i understand this hits the database
authors1 = post.authors.all()
# does this his the database again?
authors2 = post.authors.all()
# as i understand this hits the database
category1 = post.category
# does this hit the database again?
category2 = post.category
注:目前使用Django 1.3的工作,但它的好,知道什么是在其他版本。
还是不太对。 ManyToMany查询根本没有被缓存 - 它们实际上等同于反向FK查找,所以不要缓存,除非在1.4中使用新的'prefetch_related'功能。 –
谢谢。必要时我会自己处理ManyToMany字段上的缓存。 – bpscott
答案需要修复并且更好,然后进行测试。在DJ1.5中我没有看到任何一种情况下的缓存。 – Bryce