FileField deletes files even when model instance is deleted in a transaction that is rolled back
|Reported by:||durdinator||Owned by:||nobody|
|Has patch:||no||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||no|
I've got a model with a few FileFields. I've got code resembling this:
class SomeObject(models.Model): ... class RelatedFile(models.Model): filename = models.FileField(...) owner = models.ForeignKey(SomeObject, related_name='related_files') @commit_on_success def update_files(request, id): obj = get_object_or_404(SomeObject, pk=id) obj.related_files.all().delete() ... more code - which depending on external conditions can raise an exception ...
Since the FileField hooks the RelatedFile's post_delete signal, it deletes the original files when the delete() method is called. Then if the transaction is rolled back, the rows in the database, not being deleted after all, point to nonexistent files.