From b6aed803b20cc7898a82fd65845e97676276f3fa Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 27 Jun 2013 10:58:05 +0200 Subject: [PATCH] [1.6.x] Fixed #20660 -- Do not try to delete an unset FieldFile Thanks stanislas.guerra at gmail.com for the report and Baptiste Mispelon for the review. Backport of ea3fe78a9d from master. --- django/db/models/fields/files.py | 2 ++ tests/model_fields/tests.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index e631f177e9..3b3c1ec2b6 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -96,6 +96,8 @@ class FieldFile(File): save.alters_data = True def delete(self, save=True): + if not self: + return # Only close the file if it's already open, which we know by the # presence of self._file if hasattr(self, '_file'): diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index ccff8b8cfa..6abeed8c42 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -432,6 +432,17 @@ class FileFieldTests(unittest.TestCase): field.save_form_data(d, 'else.txt') self.assertEqual(d.myfile, 'else.txt') + def test_delete_when_file_unset(self): + """ + Calling delete on an unset FileField should not call the file deletion + process, but fail silently (#20660). + """ + d = Document() + try: + d.myfile.delete() + except OSError: + self.fail("Deleting an unset FileField should not raise OSError.") + class BinaryFieldTests(test.TestCase): binary_data = b'\x00\x46\xFE'