From 00e7a571c5737c69bc225ae39d3dca6d0fbfd072 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Wed, 26 Jan 2011 07:46:19 +0000 Subject: [PATCH] Fixed #6456 - Excised FileField file deletion to avoid data loss. Thanks to durdinator for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15321 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/files.py | 13 ------------- docs/releases/1.3.txt | 14 ++++++++++++++ tests/modeltests/files/tests.py | 5 ++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index a65b7cdb4c..aaf1fbb0ec 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -258,19 +258,6 @@ class FileField(Field): def contribute_to_class(self, cls, name): super(FileField, self).contribute_to_class(cls, name) setattr(cls, self.name, self.descriptor_class(self)) - signals.post_delete.connect(self.delete_file, sender=cls) - - def delete_file(self, instance, sender, **kwargs): - file = getattr(instance, self.attname) - # If no other object of this type references the file, - # and it's not the default value for future objects, - # delete it from the backend. - if file and file.name != self.default and \ - not sender._default_manager.filter(**{self.name: file.name}): - file.delete(save=False) - elif file: - # Otherwise, just close the file, so it doesn't tie up resources. - file.close() def get_directory_name(self): return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to)))) diff --git a/docs/releases/1.3.txt b/docs/releases/1.3.txt index 89e8b58585..1ca89380f7 100644 --- a/docs/releases/1.3.txt +++ b/docs/releases/1.3.txt @@ -261,6 +261,20 @@ requests. These include: Backwards-incompatible changes in 1.3 ===================================== +FileField no longer deletes files +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In earlier Django versions, when a model instance containing a +:class:`~django.db.models.FileField` was deleted, +:class:`~django.db.models.FileField` took it upon itself to also delete the +file from the backend storage. This opened the door to several data-loss +scenarios, including rolled-back transactions and fields on different models +referencing the same file. In Django 1.3, :class:`~django.db.models.FileField` +will never delete files from the backend storage. If you need cleanup of +orphaned files, you'll need to handle it yourself (for instance, with a custom +management command that can be run manually or scheduled to run periodically +via e.g. cron). + PasswordInput default rendering behavior ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py index 26808f7d9a..ad5c3a7cc3 100644 --- a/tests/modeltests/files/tests.py +++ b/tests/modeltests/files/tests.py @@ -61,11 +61,10 @@ class FileTests(TestCase): cache.set("obj2", obj2) self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt") - # Deleting an object deletes the file it uses, if there are no other - # objects still using that file. + # Deleting an object does not delete the file it uses. obj2.delete() obj2.normal.save("django_test.txt", ContentFile("more content")) - self.assertEqual(obj2.normal.name, "tests/django_test_1.txt") + self.assertEqual(obj2.normal.name, "tests/django_test_2.txt") # Multiple files with the same name get _N appended to them. objs = [Storage() for i in range(3)]