Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10363 -- Modified ModelForm handling to ensure that excluded f…

…ields can't be saved onto the model. Thanks to jgoldberg for the report, an Alex Gaynor for the fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10575 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 419ed444360cd9b843253f3901a96d41b003251e 1 parent 6590061
@freakboy3742 freakboy3742 authored
Showing with 22 additions and 2 deletions.
  1. +2 −1  django/forms/models.py
  2. +20 −1 tests/regressiontests/forms/models.py
View
3  django/forms/models.py
@@ -334,7 +334,8 @@ def save(self, commit=True):
fail_message = 'created'
else:
fail_message = 'changed'
- return save_instance(self, self.instance, self._meta.fields, fail_message, commit)
+ return save_instance(self, self.instance, self._meta.fields,
+ fail_message, commit, exclude=self._meta.exclude)
save.alters_data = True
View
21 tests/regressiontests/forms/models.py
@@ -42,7 +42,7 @@ class FileForm(django_forms.Form):
{'file1': <SimpleUploadedFile: 我隻氣墊船裝滿晒鱔.txt (text/plain)>}
>>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
-# It's enough that m gets created without error. Preservation of the exotic name is checked
+# It's enough that m gets created without error. Preservation of the exotic name is checked
# in a file_uploads test; it's hard to do that correctly with doctest's unicode issues. So
# we create and then immediately delete m so as to not leave the exotically named file around
# for shutil.rmtree (on Windows) to have trouble with later.
@@ -85,5 +85,24 @@ class FileForm(django_forms.Form):
datetime.date(1969, 4, 4)
>>> instance_form.initial['value']
12
+
+>>> from django.forms import CharField
+>>> class ExcludingForm(ModelForm):
+... name = CharField(max_length=256)
+... class Meta:
+... model = Defaults
+... exclude = ['name']
+>>> f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
+>>> f.is_valid()
+True
+>>> f.cleaned_data['name']
+u'Hello'
+>>> obj = f.save()
+>>> obj.name
+u'class default value'
+>>> obj.value
+99
+>>> obj.def_date
+datetime.date(1999, 3, 2)
>>> shutil.rmtree(temp_storage_location)
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.