Skip to content

Commit e488c1d

Browse files
committed
Fixed #12901. Again. Model validation will not be performed on excluded fields that were overridden in the form. Thanks, ammarr.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12590 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent c736cbe commit e488c1d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

django/forms/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ def _get_validation_exclusions(self):
280280
# class. See #12901.
281281
elif self._meta.fields and field not in self._meta.fields:
282282
exclude.append(f.name)
283+
elif self._meta.exclude and field in self._meta.exclude:
284+
exclude.append(f.name)
283285

284286
# Exclude fields that failed form validation. There's no need for
285287
# the model fields to validate them as well.

tests/modeltests/model_forms/tests.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from models import Category
44

55

6-
class IncompleteCategoryForm(forms.ModelForm):
6+
class IncompleteCategoryFormWithFields(forms.ModelForm):
77
"""
88
A form that replaces the model's url field with a custom one. This should
99
prevent the model field's validation from being called.
@@ -14,8 +14,24 @@ class Meta:
1414
fields = ('name', 'slug')
1515
model = Category
1616

17+
class IncompleteCategoryFormWithExclude(forms.ModelForm):
18+
"""
19+
A form that replaces the model's url field with a custom one. This should
20+
prevent the model field's validation from being called.
21+
"""
22+
url = forms.CharField(required=False)
23+
24+
class Meta:
25+
exclude = ['url']
26+
model = Category
27+
28+
1729
class ValidationTest(TestCase):
18-
def test_validates_with_replaced_field(self):
19-
form = IncompleteCategoryForm(data={'name': 'some name', 'slug': 'some-slug'})
30+
def test_validates_with_replaced_field_not_specified(self):
31+
form = IncompleteCategoryFormWithFields(data={'name': 'some name', 'slug': 'some-slug'})
32+
assert form.is_valid()
33+
34+
def test_validates_with_replaced_field_excluded(self):
35+
form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
2036
assert form.is_valid()
2137

0 commit comments

Comments
 (0)