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.
Change History (9)
comment:1 Changed 7 years ago by PhiR
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:2 Changed 6 years ago by Gulopine
- Component changed from Core framework to File uploads/storage