Ticket #15040: 15040.diff

File 15040.diff, 4.1 KB (added by Ramiro Morales, 13 years ago)

Patch, includes tests

  • django/db/models/sql/compiler.py

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    a b  
    767767                        # We only set this up here because
    768768                        # related_select_fields isn't populated until
    769769                        # execute_sql() has been called.
     770
     771                        # We also include types of fields of related models
     772                        # that will be included via select_related() for the
     773                        # benefit of MySQL/MySQLdb when boolean fields are
     774                        # involved (#15040)
    770775                        if self.query.select_fields:
    771776                            fields = self.query.select_fields + self.query.related_select_fields
    772777                        else:
    773                             fields = self.query.model._meta.fields
     778                            fields = self.query.model._meta.fields + self.query.related_select_fields
    774779                        # If the field was deferred, exclude it from being passed
    775780                        # into `resolve_columns` because it wasn't selected.
    776781                        only_load = self.deferred_to_columns()
  • tests/regressiontests/model_fields/models.py

    diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
    a b  
    6666    bfield = models.BooleanField()
    6767    string = models.CharField(max_length=10, default='abc')
    6868
     69class FksToBooleans(models.Model):
     70    """Model wih FKs to models with {Null,}BooleanField's, #15040"""
     71    bf = models.ForeignKey(BooleanModel)
     72    nbf = models.ForeignKey(NullBooleanModel)
     73
    6974class RenamedField(models.Model):
    7075    modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
    7176
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    a b  
    1111from django.utils import unittest
    1212
    1313from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post,
    14     NullBooleanModel, BooleanModel, Document, RenamedField)
     14    NullBooleanModel, BooleanModel, Document, RenamedField, FksToBooleans)
    1515
    1616# If PIL available, do these tests.
    1717if Image:
     
    211211            select={'string_length': 'LENGTH(string)'})[0]
    212212        self.assertFalse(isinstance(b5.pk, bool))
    213213
     214    def test_select_related(self):
     215        """
     216        Test type of boolean fields when retrieved via select_related() (MySQL,
     217        #15040)
     218        """
     219        bmt = BooleanModel.objects.create(bfield=True)
     220        bmf = BooleanModel.objects.create(bfield=False)
     221        nbmt = NullBooleanModel.objects.create(nbfield=True)
     222        nbmf = NullBooleanModel.objects.create(nbfield=False)
     223
     224        m1 = FksToBooleans.objects.create(bf=bmt, nbf=nbmt)
     225        m2 = FksToBooleans.objects.create(bf=bmf, nbf=nbmf)
     226
     227        # Test select_related('fk_field_name')
     228        ma = FksToBooleans.objects.select_related('bf').get(pk=m1.id)
     229        # verify types -- should't be 0/1
     230        self.assertIsInstance(ma.bf.bfield, bool)
     231        self.assertIsInstance(ma.nbf.nbfield, bool)
     232        # verify values
     233        self.assertEqual(ma.bf.bfield, True)
     234        self.assertEqual(ma.nbf.nbfield, True)
     235
     236        # Test select_related()
     237        mb = FksToBooleans.objects.select_related().get(pk=m1.id)
     238        mc = FksToBooleans.objects.select_related().get(pk=m2.id)
     239        # verify types -- shouldn't be 0/1
     240        self.assertIsInstance(mb.bf.bfield, bool)
     241        self.assertIsInstance(mb.nbf.nbfield, bool)
     242        self.assertIsInstance(mc.bf.bfield, bool)
     243        self.assertIsInstance(mc.nbf.nbfield, bool)
     244        # verify values
     245        self.assertEqual(mb.bf.bfield, True)
     246        self.assertEqual(mb.nbf.nbfield, True)
     247        self.assertEqual(mc.bf.bfield, False)
     248        self.assertEqual(mc.nbf.nbfield, False)
     249
    214250class ChoicesTests(test.TestCase):
    215251    def test_choices_and_field_display(self):
    216252        """
Back to Top