Ticket #7190: django-boolean_mysql_r12847.patch

File django-boolean_mysql_r12847.patch, 4.1 KB (added by gav, 5 years ago)
  • django/db/backends/mysql/base.py

     
    125125    related_fields_match_type = True
    126126
    127127class 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':
     
    224226        second = '%s-12-31 23:59:59.99'
    225227        return [first % value, second % value]
    226228
     229    def convert_values(self, value, field):
     230        if (field.get_internal_type() in ("BooleanField", "NullBooleanField") and
     231            value in (0, 1)):
     232            return bool(value)
     233        return value
     234
    227235class DatabaseWrapper(BaseDatabaseWrapper):
    228236
    229237    operators = {
  • django/db/backends/mysql/compiler.py

     
     1from django.db.models.sql import compiler
     2
     3class SQLCompiler(compiler.SQLCompiler):
     4    def resolve_columns(self, row, fields=()):
     5        values = []
     6        for value, field in map(None, row, fields):
     7            values.append(self.query.convert_values(value, field, connection=self.connection))
     8        return tuple(values)
     9
     10
     11class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
     12    pass
     13
     14class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
     15    pass
     16
     17class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
     18    pass
     19
     20class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
     21    pass
     22
     23class SQLDateCompiler(compiler.SQLDateCompiler, SQLCompiler):
     24    pass
  • tests/regressiontests/model_fields/tests.py

     
    66from django.db import models
    77from django.core.exceptions import ValidationError
    88
    9 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel
     9from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
    1010
    1111try:
    1212    from decimal import Decimal
     
    161161
    162162        f = models.BooleanField(choices=choices, default=1, null=False)
    163163        self.assertEqual(f.formfield().choices, choices)
     164       
     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)
    164172
     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
    165194class ChoicesTests(django.test.TestCase):
    166195    def test_choices_and_field_display(self):
    167196        """
  • tests/regressiontests/model_fields/models.py

     
    6767class NullBooleanModel(models.Model):
    6868    nbfield = models.NullBooleanField()
    6969
     70class BooleanModel(models.Model):
     71    bfield = models.BooleanField()
    7072
    7173###############################################################################
    7274# ImageField
Back to Top