1
0
Fork 0

Reorder operations in save() overrides

Accessing many-to-many relations before saving is no longer allowed.

Reorder all operations consistently:
1. Validations
2. Modify own fields
3. Perform save by calling super().save()
4. Modify related objects and clear caches

Especially clearing caches should be done after actually saving, otherwise the old data can be
re-added immediately by another request before the new data is written.
This commit is contained in:
Bart Schuurmans 2024-03-29 21:12:59 +01:00
parent 47fdad9c87
commit 0d621b68e0
7 changed files with 32 additions and 20 deletions

View file

@ -459,9 +459,10 @@ class Review(BookStatus):
def save(self, *args, **kwargs):
"""clear rating caches"""
super().save(*args, **kwargs)
if self.book.parent_work:
cache.delete(f"book-rating-{self.book.parent_work.id}")
super().save(*args, **kwargs)
class ReviewRating(Review):