+
+def _m2m_changed_handler(sender, instance, action, reverse, pk_set, **kwargs):
+ """ refresh all the short_html stuff on BookMedia delete """
+ if sender == Book.medias.through and reverse and action == 'pre_clear':
+ for book in instance.book_set.all():
+ book.save()
+m2m_changed.connect(_m2m_changed_handler)
+
+def _pre_delete_handler(sender, instance, **kwargs):
+ """ explicitly clear m2m, so that Books can be refreshed """
+ if sender == BookMedia:
+ instance.book_set.clear()
+pre_delete.connect(_pre_delete_handler)
+
+def _post_save_handler(sender, instance, **kwargs):
+ """ refresh all the short_html stuff on BookMedia update """
+ if sender == BookMedia:
+ for book in instance.book_set.all():
+ book.save()
+post_save.connect(_post_save_handler)
+