summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2016-05-18 07:30:42 -0700
committerTim Graham <timograham@gmail.com>2016-06-13 09:14:36 -0400
commit267dc4adddd2882182f71a7f285a06b1d4b15af0 (patch)
tree085fbeac6fa9139c5214bc16b46535cde690733d /tests
parentf2c0eb19e961f5864573251e70bdcdecd0250aed (diff)
Fixed #4136 -- Made ModelForm save empty values for nullable CharFields as NULL.
Previously, empty values were saved as strings.
Diffstat (limited to 'tests')
-rw-r--r--tests/model_forms/models.py4
-rw-r--r--tests/model_forms/tests.py34
2 files changed, 31 insertions, 7 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py
index 2c3ff26cbe..ec39e5ad1f 100644
--- a/tests/model_forms/models.py
+++ b/tests/model_forms/models.py
@@ -483,3 +483,7 @@ class StrictAssignmentAll(models.Model):
class Award(models.Model):
name = models.CharField(max_length=30)
character = models.ForeignKey(Character, models.SET_NULL, blank=False, null=True)
+
+
+class NullableUniqueCharFieldModel(models.Model):
+ codename = models.CharField(max_length=50, blank=True, null=True, unique=True)
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 78e7399637..297ccaae3d 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -28,9 +28,10 @@ from .models import (
CustomErrorMessage, CustomFF, CustomFieldForExclusionModel, DateTimePost,
DerivedBook, DerivedPost, Document, ExplicitPK, FilePathModel,
FlexibleDatePost, Homepage, ImprovedArticle, ImprovedArticleWithParentLink,
- Inventory, Person, Photo, Post, Price, Product, Publication,
- PublicationDefaults, StrictAssignmentAll, StrictAssignmentFieldSpecific,
- Student, StumpJoke, TextFile, Triple, Writer, WriterProfile, test_images,
+ Inventory, NullableUniqueCharFieldModel, Person, Photo, Post, Price,
+ Product, Publication, PublicationDefaults, StrictAssignmentAll,
+ StrictAssignmentFieldSpecific, Student, StumpJoke, TextFile, Triple,
+ Writer, WriterProfile, test_images,
)
if test_images:
@@ -270,6 +271,21 @@ class ModelFormBaseTest(TestCase):
obj = form.save()
self.assertEqual(obj.name, '')
+ def test_save_blank_null_unique_charfield_saves_null(self):
+ form_class = modelform_factory(model=NullableUniqueCharFieldModel, fields=['codename'])
+ empty_value = '' if connection.features.interprets_empty_strings_as_nulls else None
+
+ form = form_class(data={'codename': ''})
+ self.assertTrue(form.is_valid())
+ form.save()
+ self.assertEqual(form.instance.codename, empty_value)
+
+ # Save a second form to verify there isn't a unique constraint violation.
+ form = form_class(data={'codename': ''})
+ self.assertTrue(form.is_valid())
+ form.save()
+ self.assertEqual(form.instance.codename, empty_value)
+
def test_missing_fields_attribute(self):
message = (
"Creating a ModelForm without either the 'fields' attribute "
@@ -800,10 +816,14 @@ class UniqueTest(TestCase):
form.save()
form = ExplicitPKForm({'key': 'key1', 'desc': ''})
self.assertFalse(form.is_valid())
- self.assertEqual(len(form.errors), 3)
- self.assertEqual(form.errors['__all__'], ['Explicit pk with this Key and Desc already exists.'])
- self.assertEqual(form.errors['desc'], ['Explicit pk with this Desc already exists.'])
- self.assertEqual(form.errors['key'], ['Explicit pk with this Key already exists.'])
+ if connection.features.interprets_empty_strings_as_nulls:
+ self.assertEqual(len(form.errors), 1)
+ self.assertEqual(form.errors['key'], ['Explicit pk with this Key already exists.'])
+ else:
+ self.assertEqual(len(form.errors), 3)
+ self.assertEqual(form.errors['__all__'], ['Explicit pk with this Key and Desc already exists.'])
+ self.assertEqual(form.errors['desc'], ['Explicit pk with this Desc already exists.'])
+ self.assertEqual(form.errors['key'], ['Explicit pk with this Key already exists.'])
def test_unique_for_date(self):
p = Post.objects.create(