Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit ea3fe78a9d742904f6902cdc353a11d795418105 1 parent b5f709e
@claudep claudep authored
Showing with 13 additions and 0 deletions.
  1. +2 −0  django/db/models/fields/files.py
  2. +11 −0 tests/model_fields/tests.py
View
2  django/db/models/fields/files.py
@@ -96,6 +96,8 @@ def save(self, name, content, save=True):
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'):
View
11 tests/model_fields/tests.py
@@ -432,6 +432,17 @@ def test_changed(self):
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'

0 comments on commit ea3fe78

Please sign in to comment.
Something went wrong with that request. Please try again.