Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11149 -- Don't call save_form_data on file-type fields multipl…

…e times when saving a model form.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10826 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8c8625bde346d619befbdbb8a550368aa9b54b4d 1 parent 2e24596
Karen Tracey authored May 19, 2009
8  django/forms/models.py
@@ -50,14 +50,14 @@ def save_instance(form, instance, fields=None, fail_message='saved',
50 50
             continue
51 51
         if exclude and f.name in exclude:
52 52
             continue
  53
+        # OneToOneField doesn't allow assignment of None. Guard against that
  54
+        # instead of allowing it and throwing an error.
  55
+        if isinstance(f, models.OneToOneField) and cleaned_data[f.name] is None:
  56
+            continue
53 57
         # Defer saving file-type fields until after the other fields, so a
54 58
         # callable upload_to can use the values from other fields.
55 59
         if isinstance(f, models.FileField):
56 60
             file_field_list.append(f)
57  
-        # OneToOneField doesn't allow assignment of None. Guard against that
58  
-        # instead of allowing it and throwing an error.
59  
-        if isinstance(f, models.OneToOneField) and cleaned_data[f.name] is None:
60  
-            pass
61 61
         else:
62 62
             f.save_form_data(instance, cleaned_data[f.name])
63 63
 
9  tests/regressiontests/model_forms_regress/models.py
@@ -28,3 +28,12 @@ class Article(models.Model):
28 28
 
29 29
     def __unicode__(self):
30 30
         return self.headline
  31
+
  32
+class CustomFileField(models.FileField):
  33
+    def save_form_data(self, instance, data):
  34
+        been_here = getattr(self, 'been_saved', False)
  35
+        assert not been_here, "save_form_data called more than once"
  36
+        setattr(self, 'been_saved', True)
  37
+
  38
+class CustomFF(models.Model):
  39
+    f = CustomFileField(upload_to='unused', blank=True)
15  tests/regressiontests/model_forms_regress/tests.py
@@ -6,7 +6,7 @@
6 6
 from django.conf import settings
7 7
 from django.test import TestCase
8 8
 
9  
-from models import Person, Triple, FilePathModel, Article, Publication
  9
+from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
10 10
 
11 11
 class ModelMultipleChoiceFieldTests(TestCase):
12 12
 
@@ -88,3 +88,16 @@ def formfield_for_dbfield(db_field, **kwargs):
88 88
 <option value="2" selected="selected">Second Book</option>
89 89
 <option value="3">Third Book</option>
90 90
 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>""")
  91
+
  92
+class CFFForm(forms.ModelForm):
  93
+    class Meta:
  94
+        model = CustomFF
  95
+
  96
+class CustomFieldSaveTests(TestCase):
  97
+    def test_save(self):
  98
+        "Regression for #11149: save_form_data should be called only once"
  99
+        
  100
+        # It's enough that the form saves without error -- the custom save routine will
  101
+        # generate an AssertionError if it is called more than once during save.
  102
+        form = CFFForm(data = {'f': None})
  103
+        form.save()

0 notes on commit 8c8625b

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