简体   繁体   中英

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. 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. 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. The prefetch_related('toppings') implied pizza.toppings.all() , but pizza.toppings.filter() is a new and different query. 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.

From docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM