Ticket #21413: 21413.diff

File 21413.diff, 3.1 KB (added by slide21@…, 11 years ago)

patch for ticket #21413

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

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    index 38806e7..b655990 100644
    a b class SQLCompiler(object):  
    647647                               else None)
    648648                columns, aliases = self.get_default_columns(start_alias=alias,
    649649                    opts=model._meta, as_pairs=True, from_parent=from_parent)
     650               
     651                fields = []
     652                for field, field_model in model._meta.get_concrete_fields_with_model():
     653                    if from_parent and field_model is not None and issubclass(from_parent, field_model):
     654                        # Avoid loading data for already loaded parents.
     655                        continue
     656                    fields.append(field)
     657
    650658                self.query.related_select_cols.extend(
    651659                    SelectInfo(col, field) for col, field
    652                     in zip(columns, model._meta.concrete_fields))
     660                    in zip(columns, fields))
    653661                next = requested.get(f.related_query_name(), {})
    654662                self.fill_related_selections(model._meta, alias, cur_depth + 1,
    655663                                             next, restricted)
  • tests/select_related_regress/models.py

    diff --git a/tests/select_related_regress/models.py b/tests/select_related_regress/models.py
    index c2611b4..102f1e8 100644
    a b class C(Base):  
    154154    c_a = models.ForeignKey(A)
    155155    c_b = models.ForeignKey(B)
    156156    is_published = models.BooleanField(default=False)
     157   
     158
     159class Place(models.Model):
     160    name = models.CharField(max_length=50)
     161    address = models.CharField(max_length=80)
     162
     163class Restaurant(Place):
     164    serves_hot_dogs = models.BooleanField(default=False)
     165    serves_pizza = models.BooleanField(default=False)
  • tests/select_related_regress/tests.py

    diff --git a/tests/select_related_regress/tests.py b/tests/select_related_regress/tests.py
    index 78d5838..9f80667 100644
    a b from django.utils import six  
    55
    66from .models import (Building, Child, Device, Port, Item, Country, Connection,
    77    ClientStatus, State, Client, SpecialClient, TUser, Person, Student,
    8     Organizer, Class, Enrollment, Hen, Chick, A, B, C)
     8    Organizer, Class, Enrollment, Hen, Chick, A, B, C, Place, Restaurant)
    99
    1010
    1111class SelectRelatedRegressTests(TestCase):
    class SelectRelatedRegressTests(TestCase):  
    188188            self.assertEqual(qs_c.c_b.lots_of_text, 'lots_of_text_b')
    189189            self.assertEqual(qs_c.c_a.name, 'a')
    190190            self.assertEqual(qs_c.c_b.name, 'b')
     191           
     192    def test_ticket_21413(self):
     193        Restaurant.objects.create(name='Bobs Cafe', address="Somewhere",
     194            serves_pizza=True, serves_hot_dogs=True)
     195        restaurant = Place.objects.all().select_related('restaurant')[0].restaurant
     196        self.assertIsInstance(restaurant.serves_pizza, bool)
     197        self.assertIsInstance(restaurant.serves_hot_dogs, bool)
     198   
Back to Top