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
This commit is contained in:
parent
e434573ef1
commit
7471dab660
|
@ -292,13 +292,16 @@ class BaseModelForm(BaseForm):
|
||||||
elif field in self._errors.keys():
|
elif field in self._errors.keys():
|
||||||
exclude.append(f.name)
|
exclude.append(f.name)
|
||||||
|
|
||||||
# Exclude empty fields that are not required by the form. The
|
# Exclude empty fields that are not required by the form, if the
|
||||||
# underlying model field may be required, so this keeps the model
|
# underlying model field is required. This keeps the model field
|
||||||
# field from raising that error.
|
# from raising a required error. Note: don't exclude the field from
|
||||||
|
# validaton if the model field allows blanks. If it does, the blank
|
||||||
|
# value may be included in a unique check, so cannot be excluded
|
||||||
|
# from validation.
|
||||||
else:
|
else:
|
||||||
form_field = self.fields[field]
|
form_field = self.fields[field]
|
||||||
field_value = self.cleaned_data.get(field, None)
|
field_value = self.cleaned_data.get(field, None)
|
||||||
if field_value is None and not form_field.required:
|
if not f.blank and not form_field.required and field_value in EMPTY_VALUES:
|
||||||
exclude.append(f.name)
|
exclude.append(f.name)
|
||||||
return exclude
|
return exclude
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
from django import forms
|
||||||
from django.forms import ModelForm
|
from django.forms import ModelForm
|
||||||
|
|
||||||
from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost
|
from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost, Writer
|
||||||
|
|
||||||
class ProductForm(ModelForm):
|
class ProductForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -30,3 +31,9 @@ class PostForm(ModelForm):
|
||||||
class DerivedPostForm(ModelForm):
|
class DerivedPostForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DerivedPost
|
model = DerivedPost
|
||||||
|
|
||||||
|
class CustomWriterForm(ModelForm):
|
||||||
|
name = forms.CharField(required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Writer
|
||||||
|
|
|
@ -2,7 +2,8 @@ import datetime
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django import forms
|
from django import forms
|
||||||
from models import Category, Writer, Book, DerivedBook, Post
|
from models import Category, Writer, Book, DerivedBook, Post
|
||||||
from mforms import ProductForm, PriceForm, BookForm, DerivedBookForm, ExplicitPKForm, PostForm, DerivedPostForm
|
from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
|
||||||
|
ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm)
|
||||||
|
|
||||||
|
|
||||||
class IncompleteCategoryFormWithFields(forms.ModelForm):
|
class IncompleteCategoryFormWithFields(forms.ModelForm):
|
||||||
|
@ -37,6 +38,10 @@ class ValidationTest(TestCase):
|
||||||
form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
|
form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
|
||||||
assert form.is_valid()
|
assert form.is_valid()
|
||||||
|
|
||||||
|
def test_notrequired_overrides_notblank(self):
|
||||||
|
form = CustomWriterForm({})
|
||||||
|
assert form.is_valid()
|
||||||
|
|
||||||
# unique/unique_together validation
|
# unique/unique_together validation
|
||||||
class UniqueTest(TestCase):
|
class UniqueTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue