首页 > 解决方案 > django:刷新查询集结果缓存的最新方法是什么?

问题描述

当前(2.0) 版本的 Django 中,如今强制 Django 忽略它可能包含的任何查询集结果缓存并从数据库中重新检索信息的最佳方法是什么?

https://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets ...明确谈论缓存,但实际上并没有说明如何强制清空它。

我宁愿以“官方”的方式做到这一点。 当然必须有一个。

标签: djangocachingdjango-queryset

解决方案


不确定我是否遗漏了什么,但忽略缓存意味着再次获取数据。根据您链接的文档页面

在新创建的 QuerySet 中,缓存是空的。

所以,而不是例子

queryset = Entry.objects.all()
print([p.headline for p in queryset]) # Evaluate the query set.
print([p.pub_date for p in queryset]) # Re-use the cache from the evaluation.

queryset = Entry.objects.all()
print([p.headline for p in queryset]) # Evaluate the query set.
queryset = Entry.objects.all()
print([p.pub_date for p in queryset]) # Evaluate the query set again.

推荐阅读