【发布时间】:2021-10-23 03:09:54
【问题描述】:
这是模型:
class Category(models.Model):
name = models.TextField()
class Post(models.Model):
category = models.ForeignKey(Category)
现在,我想获取某个类别的帖子:
category = Category.objects.get(id=1)
posts = category.post_set.all()
# this line hit the DB
posts = category.post_set.all()
# and this line hit the DB again!
如何在这些关系中使用缓存的结果。我使用 Django rest-framework,它使 DB 为每个实例多次命中。
【问题讨论】:
标签: python django django-rest-framework django-orm