Ticket #15040: 15040.diff
File 15040.diff, 4.1 KB (added by , 13 years ago) |
---|
-
django/db/models/sql/compiler.py
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
a b 767 767 # We only set this up here because 768 768 # related_select_fields isn't populated until 769 769 # 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) 770 775 if self.query.select_fields: 771 776 fields = self.query.select_fields + self.query.related_select_fields 772 777 else: 773 fields = self.query.model._meta.fields 778 fields = self.query.model._meta.fields + self.query.related_select_fields 774 779 # If the field was deferred, exclude it from being passed 775 780 # into `resolve_columns` because it wasn't selected. 776 781 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 66 66 bfield = models.BooleanField() 67 67 string = models.CharField(max_length=10, default='abc') 68 68 69 class 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 69 74 class RenamedField(models.Model): 70 75 modelname = models.IntegerField(name="fieldname", choices=((1,'One'),)) 71 76 -
tests/regressiontests/model_fields/tests.py
diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
a b 11 11 from django.utils import unittest 12 12 13 13 from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, 14 NullBooleanModel, BooleanModel, Document, RenamedField )14 NullBooleanModel, BooleanModel, Document, RenamedField, FksToBooleans) 15 15 16 16 # If PIL available, do these tests. 17 17 if Image: … … 211 211 select={'string_length': 'LENGTH(string)'})[0] 212 212 self.assertFalse(isinstance(b5.pk, bool)) 213 213 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 214 250 class ChoicesTests(test.TestCase): 215 251 def test_choices_and_field_display(self): 216 252 """