Opened 9 years ago
Closed 9 years ago
#26767 closed Bug (duplicate)
Modifications to a prefetched many-to-many field should clear the cache
Reported by: | Matt d'Entremont | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.9 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
We were running into issues with stale data, and discovered that the cause was prefetched data sticking around after field updates.
Example code:
class Books(Model): ... author = models.ForeignKey('Author', related_name='books') class Author(Model) ... first_author = Author.objects.prefetch_related('books')[0] first_author.books = [] print first_author.books.count() # Gotcha!
After performing the first_author.books = []
, the proper changes are made to the through model. However, when performing first_author.books.count()
, the result may not be 0
; but will instead be the number of books associated with the author before the first_author.books = []
statement.
What would be nice would be either:
- Update the prefetched cache for a field when a the field is changed
- Clear the prefetched cache for a field when the field is changed
Note:
See TracTickets
for help on using tickets.
Duplicate of #26706