the django pre-delete signal is not updating the foreign key field of the instance

The book in your delete view is a different Python object than the instance.book in your signal handler. The Python object does not magically learn that the underlying db representation has changed. You could call refresh_from_db before printing:

 book.refresh_from_db()
 print(book.num_of_likes)

Or just make num_of_likes an dynamically evaluated property altogether and you won’t have to worry about the integrity of your denormalized data:

class Book(models.Model):
    @property
    def num_of_likes(self):
        return self.like_set.count()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top