Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 7471dab660d916ab2ebd6b943aec6c2b1145e744 1 parent e434573
Karen Tracey authored March 18, 2010
11  django/forms/models.py
@@ -292,13 +292,16 @@ def _get_validation_exclusions(self):
292 292
             elif field in self._errors.keys():
293 293
                 exclude.append(f.name)
294 294
 
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.
298 301
             else:
299 302
                 form_field = self.fields[field]
300 303
                 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:
302 305
                     exclude.append(f.name)
303 306
         return exclude
304 307
 
9  tests/modeltests/model_forms/mforms.py
... ...
@@ -1,6 +1,7 @@
  1
+from django import forms
1 2
 from django.forms import ModelForm
2 3
 
3  
-from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost
  4
+from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost, Writer
4 5
 
5 6
 class ProductForm(ModelForm):
6 7
     class Meta:
@@ -30,3 +31,9 @@ class Meta:
30 31
 class DerivedPostForm(ModelForm):
31 32
     class Meta:
32 33
         model = DerivedPost
  34
+
  35
+class CustomWriterForm(ModelForm):
  36
+   name = forms.CharField(required=False)
  37
+
  38
+   class Meta:
  39
+       model = Writer
7  tests/modeltests/model_forms/tests.py
@@ -2,7 +2,8 @@
2 2
 from django.test import TestCase
3 3
 from django import forms
4 4
 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)
6 7
 
7 8
 
8 9
 class IncompleteCategoryFormWithFields(forms.ModelForm):
@@ -37,6 +38,10 @@ def test_validates_with_replaced_field_excluded(self):
37 38
         form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
38 39
         assert form.is_valid()
39 40
 
  41
+    def test_notrequired_overrides_notblank(self):
  42
+        form = CustomWriterForm({})
  43
+        assert form.is_valid()
  44
+
40 45
 # unique/unique_together validation
41 46
 class UniqueTest(TestCase):
42 47
     def setUp(self):

0 notes on commit 7471dab

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