Skip to content

Commit 7471dab

Browse files
committed
Fixed #13138: Ensure required=False on a model form field overrides
blank=False on the underlying model field during model form clean, in order to maintain backward compatibility. Thanks to saxon75 for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12802 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent e434573 commit 7471dab

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

django/forms/models.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,16 @@ def _get_validation_exclusions(self):
292292
elif field in self._errors.keys():
293293
exclude.append(f.name)
294294

295-
# Exclude empty fields that are not required by the form. The
296-
# underlying model field may be required, so this keeps the model
297-
# field from raising that error.
295+
# Exclude empty fields that are not required by the form, if the
296+
# underlying model field is required. This keeps the model field
297+
# from raising a required error. Note: don't exclude the field from
298+
# validaton if the model field allows blanks. If it does, the blank
299+
# value may be included in a unique check, so cannot be excluded
300+
# from validation.
298301
else:
299302
form_field = self.fields[field]
300303
field_value = self.cleaned_data.get(field, None)
301-
if field_value is None and not form_field.required:
304+
if not f.blank and not form_field.required and field_value in EMPTY_VALUES:
302305
exclude.append(f.name)
303306
return exclude
304307

tests/modeltests/model_forms/mforms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from django import forms
12
from django.forms import ModelForm
23

3-
from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost
4+
from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost, Writer
45

56
class ProductForm(ModelForm):
67
class Meta:
@@ -30,3 +31,9 @@ class Meta:
3031
class DerivedPostForm(ModelForm):
3132
class Meta:
3233
model = DerivedPost
34+
35+
class CustomWriterForm(ModelForm):
36+
name = forms.CharField(required=False)
37+
38+
class Meta:
39+
model = Writer

tests/modeltests/model_forms/tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from django.test import TestCase
33
from django import forms
44
from models import Category, Writer, Book, DerivedBook, Post
5-
from mforms import ProductForm, PriceForm, BookForm, DerivedBookForm, ExplicitPKForm, PostForm, DerivedPostForm
5+
from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
6+
ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm)
67

78

89
class IncompleteCategoryFormWithFields(forms.ModelForm):
@@ -37,6 +38,10 @@ def test_validates_with_replaced_field_excluded(self):
3738
form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
3839
assert form.is_valid()
3940

41+
def test_notrequired_overrides_notblank(self):
42+
form = CustomWriterForm({})
43+
assert form.is_valid()
44+
4045
# unique/unique_together validation
4146
class UniqueTest(TestCase):
4247
def setUp(self):

0 commit comments

Comments
 (0)