[英]Django prefetch_related cache not reflecting the changes
I have a model Banks . 我有一个样板银行 。 and I have six entities in it. 我有六个实体 。
This is my prefetch code. 这是我的预取代码。
queryset = Banks.objects.all().prefetch_related('field1','field2')
when I enter new entity into my Banks model and get the data, I still get only the old data. 当我在Banks模型中输入新实体并获取数据时,我仍然仅获得旧数据。 The newly added entity is not getting reflected when I download the data. 下载数据时,新添加的实体未得到反映。
Remember that, as always with QuerySets, any subsequent chained methods which imply a different database query will ignore previously cached results, and retrieve data using a fresh database query. 请记住,与QuerySet一样,任何暗示不同数据库查询的后续链接方法都将忽略先前缓存的结果,并使用新的数据库查询来检索数据。 So, if you write the following: 因此,如果您编写以下内容:
>>> pizzas = Pizza.objects.prefetch_related('toppings')
>>> [list(pizza.toppings.filter(spicy=True)) for pizza in pizzas]
…then the fact that pizza.toppings.all()
has been prefetched will not help you. …然后预取pizza.toppings.all()
的事实将无济于事。 The prefetch_related('toppings')
implied pizza.toppings.all()
, but pizza.toppings.filter()
is a new and different query. prefetch_related('toppings')
pizza.toppings.all()
prefetch_related('toppings')
暗示pizza.toppings.all()
,但pizza.toppings.filter()
是一个新的不同查询。 The prefetched cache can't help here; 预取的缓存在这里无济于事。 in fact it hurts performance, since you have done a database query that you haven't used. 实际上,这会影响性能,因为您已经执行了未使用的数据库查询。 So use this feature with caution! 因此,请谨慎使用此功能!
Also, if you call the database-altering methods add()
, remove()
, clear()
or set()
, on related managers, any prefetched cache for the relation will be cleared. 另外,如果在相关管理器上调用数据库更改方法add()
, remove()
, clear()
或set()
,则将清除该关系的任何预取缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.