Skip to content

Commit 271bcda

Browse files
committed
Fixed #7190 -- Corrected a problem with Boolean value handling on the MySQL backend. Thanks to George Vilches for the initial patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12900 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 2fa2cf0 commit 271bcda

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

django/db/backends/mysql/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
125125
related_fields_match_type = True
126126

127127
class DatabaseOperations(BaseDatabaseOperations):
128+
compiler_module = "django.db.backends.mysql.compiler"
129+
128130
def date_extract_sql(self, lookup_type, field_name):
129131
# http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
130132
if lookup_type == 'week_day':

django/db/backends/mysql/compiler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.db.models.sql import compiler
2+
3+
class SQLCompiler(compiler.SQLCompiler):
4+
def resolve_columns(self, row, fields=()):
5+
values = []
6+
for value, field in map(None, row, fields):
7+
if (field and field.get_internal_type() in ("BooleanField", "NullBooleanField") and
8+
value in (0, 1)):
9+
value = bool(value)
10+
values.append(value)
11+
return tuple(values)
12+
13+
14+
class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
15+
pass
16+
17+
class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
18+
pass
19+
20+
class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
21+
pass
22+
23+
class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
24+
pass
25+
26+
class SQLDateCompiler(compiler.SQLDateCompiler, SQLCompiler):
27+
pass

tests/regressiontests/model_fields/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Post(models.Model):
6767
class NullBooleanModel(models.Model):
6868
nbfield = models.NullBooleanField()
6969

70+
class BooleanModel(models.Model):
71+
bfield = models.BooleanField()
7072

7173
###############################################################################
7274
# ImageField

tests/regressiontests/model_fields/tests.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.db import models
77
from django.core.exceptions import ValidationError
88

9-
from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel
9+
from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
1010

1111
try:
1212
from decimal import Decimal
@@ -44,7 +44,7 @@ def test_nullbooleanfield_blank(self):
4444
"""
4545
Regression test for #13071: NullBooleanField should not throw
4646
a validation error when given a value of None.
47-
47+
4848
"""
4949
nullboolean = NullBooleanModel(nbfield=None)
5050
try:
@@ -162,6 +162,35 @@ def test_booleanfield_choices_blank(self):
162162
f = models.BooleanField(choices=choices, default=1, null=False)
163163
self.assertEqual(f.formfield().choices, choices)
164164

165+
def test_return_type(self):
166+
b = BooleanModel()
167+
b.bfield = True
168+
b.save()
169+
b2 = BooleanModel.objects.get(pk=b.pk)
170+
self.assertTrue(isinstance(b2.bfield, bool))
171+
self.assertEqual(b2.bfield, True)
172+
173+
b3 = BooleanModel()
174+
b3.bfield = False
175+
b3.save()
176+
b4 = BooleanModel.objects.get(pk=b3.pk)
177+
self.assertTrue(isinstance(b4.bfield, bool))
178+
self.assertEqual(b4.bfield, False)
179+
180+
b = NullBooleanModel()
181+
b.nbfield = True
182+
b.save()
183+
b2 = NullBooleanModel.objects.get(pk=b.pk)
184+
self.assertTrue(isinstance(b2.nbfield, bool))
185+
self.assertEqual(b2.nbfield, True)
186+
187+
b3 = NullBooleanModel()
188+
b3.nbfield = False
189+
b3.save()
190+
b4 = NullBooleanModel.objects.get(pk=b3.pk)
191+
self.assertTrue(isinstance(b4.nbfield, bool))
192+
self.assertEqual(b4.nbfield, False)
193+
165194
class ChoicesTests(django.test.TestCase):
166195
def test_choices_and_field_display(self):
167196
"""

0 commit comments

Comments
 (0)