From 271bcda04af23bd4deb9656edd59038537f301c5 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 1 Apr 2010 15:10:53 +0000 Subject: [PATCH] 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 --- django/db/backends/mysql/base.py | 2 ++ django/db/backends/mysql/compiler.py | 27 ++++++++++++++++ tests/regressiontests/model_fields/models.py | 2 ++ tests/regressiontests/model_fields/tests.py | 33 ++++++++++++++++++-- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 django/db/backends/mysql/compiler.py diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 2a5acad0c0..66c13ed035 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -125,6 +125,8 @@ class DatabaseFeatures(BaseDatabaseFeatures): related_fields_match_type = True class DatabaseOperations(BaseDatabaseOperations): + compiler_module = "django.db.backends.mysql.compiler" + def date_extract_sql(self, lookup_type, field_name): # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html if lookup_type == 'week_day': diff --git a/django/db/backends/mysql/compiler.py b/django/db/backends/mysql/compiler.py new file mode 100644 index 0000000000..07832f27f0 --- /dev/null +++ b/django/db/backends/mysql/compiler.py @@ -0,0 +1,27 @@ +from django.db.models.sql import compiler + +class SQLCompiler(compiler.SQLCompiler): + def resolve_columns(self, row, fields=()): + values = [] + for value, field in map(None, row, fields): + if (field and field.get_internal_type() in ("BooleanField", "NullBooleanField") and + value in (0, 1)): + value = bool(value) + values.append(value) + return tuple(values) + + +class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler): + pass + +class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler): + pass + +class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler): + pass + +class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler): + pass + +class SQLDateCompiler(compiler.SQLDateCompiler, SQLCompiler): + pass diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py index 0e365fec30..5e480aa9a5 100644 --- a/tests/regressiontests/model_fields/models.py +++ b/tests/regressiontests/model_fields/models.py @@ -67,6 +67,8 @@ class Post(models.Model): class NullBooleanModel(models.Model): nbfield = models.NullBooleanField() +class BooleanModel(models.Model): + bfield = models.BooleanField() ############################################################################### # ImageField diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py index 9d50daf871..05b710d885 100644 --- a/tests/regressiontests/model_fields/tests.py +++ b/tests/regressiontests/model_fields/tests.py @@ -6,7 +6,7 @@ from django import forms from django.db import models from django.core.exceptions import ValidationError -from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel +from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel try: from decimal import Decimal @@ -44,7 +44,7 @@ class BasicFieldTests(django.test.TestCase): """ Regression test for #13071: NullBooleanField should not throw a validation error when given a value of None. - + """ nullboolean = NullBooleanModel(nbfield=None) try: @@ -162,6 +162,35 @@ class BooleanFieldTests(unittest.TestCase): f = models.BooleanField(choices=choices, default=1, null=False) self.assertEqual(f.formfield().choices, choices) + def test_return_type(self): + b = BooleanModel() + b.bfield = True + b.save() + b2 = BooleanModel.objects.get(pk=b.pk) + self.assertTrue(isinstance(b2.bfield, bool)) + self.assertEqual(b2.bfield, True) + + b3 = BooleanModel() + b3.bfield = False + b3.save() + b4 = BooleanModel.objects.get(pk=b3.pk) + self.assertTrue(isinstance(b4.bfield, bool)) + self.assertEqual(b4.bfield, False) + + b = NullBooleanModel() + b.nbfield = True + b.save() + b2 = NullBooleanModel.objects.get(pk=b.pk) + self.assertTrue(isinstance(b2.nbfield, bool)) + self.assertEqual(b2.nbfield, True) + + b3 = NullBooleanModel() + b3.nbfield = False + b3.save() + b4 = NullBooleanModel.objects.get(pk=b3.pk) + self.assertTrue(isinstance(b4.nbfield, bool)) + self.assertEqual(b4.nbfield, False) + class ChoicesTests(django.test.TestCase): def test_choices_and_field_display(self): """