Fixed #20895 -- Made check management command warn if a BooleanField does not have a default value

Thanks to Collin Anderson for the suggestion and Tim Graham for
reviewing the patch.
This commit is contained in:
Alasdair Nicol 2013-08-11 21:19:09 +01:00 committed by Tim Graham
parent 55339a7669
commit 22c6497f99
23 changed files with 111 additions and 42 deletions

View file

@ -12,7 +12,7 @@ from django.db.models.fields import (
AutoField, BigIntegerField, BinaryField, BooleanField, CharField,
CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField,
EmailField, FilePathField, FloatField, IntegerField, IPAddressField,
GenericIPAddressField, NullBooleanField, PositiveIntegerField,
GenericIPAddressField, NOT_PROVIDED, NullBooleanField, PositiveIntegerField,
PositiveSmallIntegerField, SlugField, SmallIntegerField, TextField,
TimeField, URLField)
from django.db.models.fields.files import FileField, ImageField
@ -275,10 +275,23 @@ class BooleanFieldTests(unittest.TestCase):
Check that a BooleanField defaults to None -- which isn't
a valid value (#15124).
"""
b = BooleanModel()
self.assertIsNone(b.bfield)
with self.assertRaises(IntegrityError):
b.save()
# Patch the boolean field's default value. We give it a default
# value when defining the model to satisfy the check tests
# #20895.
boolean_field = BooleanModel._meta.get_field('bfield')
self.assertTrue(boolean_field.has_default())
old_default = boolean_field.default
try:
boolean_field.default = NOT_PROVIDED
# check patch was succcessful
self.assertFalse(boolean_field.has_default())
b = BooleanModel()
self.assertIsNone(b.bfield)
with self.assertRaises(IntegrityError):
b.save()
finally:
boolean_field.default = old_default
nb = NullBooleanModel()
self.assertIsNone(nb.nbfield)
nb.save() # no error