Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Jacob Kaplan-Moss authored April 10, 2009
8  django/db/models/fields/__init__.py
@@ -393,7 +393,13 @@ def get_db_prep_value(self, value):
393 393
         return bool(value)
394 394
 
395 395
     def formfield(self, **kwargs):
396  
-        defaults = {'form_class': forms.BooleanField}
  396
+        # Unlike most fields, BooleanField figures out include_blank from
  397
+        # self.null instead of self.blank.
  398
+        if self.choices:
  399
+            include_blank = self.null or not (self.has_default() or 'initial' in kwargs)
  400
+            defaults = {'choices': self.get_choices(include_blank=include_blank)}
  401
+        else:
  402
+            defaults = {'form_class': forms.BooleanField}
397 403
         defaults.update(kwargs)
398 404
         return super(BooleanField, self).formfield(**defaults)
399 405
 
15  tests/regressiontests/model_fields/tests.py
... ...
@@ -1,6 +1,7 @@
1 1
 import datetime
2 2
 import unittest
3 3
 import django.test
  4
+from django import forms
4 5
 from django.db import models
5 6
 from django.core.exceptions import ValidationError
6 7
 from models import Foo, Bar, Whiz, BigD, BigS
@@ -83,10 +84,22 @@ def _test_get_db_prep_lookup(self, f):
83 84
 
84 85
     def test_booleanfield_get_db_prep_lookup(self):
85 86
         self._test_get_db_prep_lookup(models.BooleanField())
86  
-        
  87
+
87 88
     def test_nullbooleanfield_get_db_prep_lookup(self):
88 89
         self._test_get_db_prep_lookup(models.NullBooleanField())
89 90
 
  91
+    def test_booleanfield_choices_blank(self):
  92
+        """
  93
+        Test that BooleanField with choices and defaults doesn't generate a
  94
+        formfield with the blank option (#9640, #10549).
  95
+        """
  96
+        choices = [(1, u'Si'), (2, 'No')]
  97
+        f = models.BooleanField(choices=choices, default=1, null=True)
  98
+        self.assertEqual(f.formfield().choices, [('', '---------')] + choices)
  99
+
  100
+        f = models.BooleanField(choices=choices, default=1, null=False)
  101
+        self.assertEqual(f.formfield().choices, choices)
  102
+
90 103
 class ChoicesTests(django.test.TestCase):
91 104
     def test_choices_and_field_display(self):
92 105
         """

0 notes on commit 2ab4028

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