Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Claude Paroz authored June 27, 2013
2  django/db/models/fields/files.py
@@ -96,6 +96,8 @@ def save(self, name, content, save=True):
96 96
     save.alters_data = True
97 97
 
98 98
     def delete(self, save=True):
  99
+        if not self:
  100
+            return
99 101
         # Only close the file if it's already open, which we know by the
100 102
         # presence of self._file
101 103
         if hasattr(self, '_file'):
11  tests/model_fields/tests.py
@@ -432,6 +432,17 @@ def test_changed(self):
432 432
         field.save_form_data(d, 'else.txt')
433 433
         self.assertEqual(d.myfile, 'else.txt')
434 434
 
  435
+    def test_delete_when_file_unset(self):
  436
+        """
  437
+        Calling delete on an unset FileField should not call the file deletion
  438
+        process, but fail silently (#20660).
  439
+        """
  440
+        d = Document()
  441
+        try:
  442
+            d.myfile.delete()
  443
+        except OSError:
  444
+            self.fail("Deleting an unset FileField should not raise OSError.")
  445
+
435 446
 
436 447
 class BinaryFieldTests(test.TestCase):
437 448
     binary_data = b'\x00\x46\xFE'

0 notes on commit ea3fe78

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