Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #9640, #10549: BooleanFields with choices, a default, a…

…nd null=False now correctly doesn't generate a blank option. Backport of r10500 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10502 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2ab40287bc8996d2cba1edd562fbe648c880ad90 1 parent 7a283f7
@jacobian jacobian authored
View
8 django/db/models/fields/__init__.py
@@ -393,7 +393,13 @@ def get_db_prep_value(self, value):
return bool(value)
def formfield(self, **kwargs):
- defaults = {'form_class': forms.BooleanField}
+ # Unlike most fields, BooleanField figures out include_blank from
+ # self.null instead of self.blank.
+ if self.choices:
+ include_blank = self.null or not (self.has_default() or 'initial' in kwargs)
+ defaults = {'choices': self.get_choices(include_blank=include_blank)}
+ else:
+ defaults = {'form_class': forms.BooleanField}
defaults.update(kwargs)
return super(BooleanField, self).formfield(**defaults)
View
15 tests/regressiontests/model_fields/tests.py
@@ -1,6 +1,7 @@
import datetime
import unittest
import django.test
+from django import forms
from django.db import models
from django.core.exceptions import ValidationError
from models import Foo, Bar, Whiz, BigD, BigS
@@ -83,10 +84,22 @@ def _test_get_db_prep_lookup(self, f):
def test_booleanfield_get_db_prep_lookup(self):
self._test_get_db_prep_lookup(models.BooleanField())
-
+
def test_nullbooleanfield_get_db_prep_lookup(self):
self._test_get_db_prep_lookup(models.NullBooleanField())
+ def test_booleanfield_choices_blank(self):
+ """
+ Test that BooleanField with choices and defaults doesn't generate a
+ formfield with the blank option (#9640, #10549).
+ """
+ choices = [(1, u'Si'), (2, 'No')]
+ f = models.BooleanField(choices=choices, default=1, null=True)
+ self.assertEqual(f.formfield().choices, [('', '---------')] + choices)
+
+ f = models.BooleanField(choices=choices, default=1, null=False)
+ self.assertEqual(f.formfield().choices, choices)
+
class ChoicesTests(django.test.TestCase):
def test_choices_and_field_display(self):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.