Ticket #7246: 7246-model-inheritance-select-related.patch

File 7246-model-inheritance-select-related.patch, 3.3 KB (added by Jacob, 16 years ago)
  • django/db/models/sql/query.py

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    index 3044882..bed996a 100644
    a b class Query(object):  
    880880
    881881        for f, model in opts.get_fields_with_model():
    882882            if (not f.rel or (restricted and f.name not in requested) or
    883                     (not restricted and f.null) or f.rel.parent_link):
     883                    (not restricted and f.null)):
    884884                continue
    885885            table = f.rel.to._meta.db_table
    886886            if nullable or f.null:
    class Query(object):  
    898898                            promote=promote)
    899899            else:
    900900                alias = root_alias
     901
     902            # For parent links all tables are already joined in "if model:" clause above
     903            # Also we don't need to recurse further along parent links
     904            if f.rel.parent_link:
     905                continue
     906
    901907            alias = self.join((alias, table, f.column,
    902908                    f.rel.get_related_field().column), exclusions=used,
    903909                    promote=promote)
    904910            used.add(alias)
    905911            self.related_select_cols.extend([(alias, f2.column)
    906                     for f2 in f.rel.to._meta.fields])
    907             self.related_select_fields.extend(f.rel.to._meta.fields)
     912                    for f2 in f.rel.to._meta.local_fields])
     913            self.related_select_fields.extend(f.rel.to._meta.local_fields)
    908914            if restricted:
    909915                next = requested.get(f.name, {})
    910916            else:
  • new file tests/regressiontests/model_inheritance_select_related/models.py

    diff --git a/tests/regressiontests/model_inheritance_select_related/__init__.py b/tests/regressiontests/model_inheritance_select_related/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/model_inheritance_select_related/models.py b/tests/regressiontests/model_inheritance_select_related/models.py
    new file mode 100644
    index 0000000..a678562
    - +  
     1"""
     2Regression tests for the interaction between model inheritance and
     3select_related().
     4"""
     5
     6from django.db import models
     7
     8class Place(models.Model):
     9    name = models.CharField(max_length=50)
     10
     11    class Meta:
     12        ordering = ('name',)
     13       
     14    def __unicode__(self):
     15        return u"%s the place" % self.name
     16
     17class Restaurant(Place):
     18    serves_suhsi = models.BooleanField()
     19    serves_steak = models.BooleanField()
     20
     21    def __unicode__(self):
     22        return u"%s the restaurant" % self.name
     23
     24class Person(models.Model):
     25    name = models.CharField(max_length=50)
     26    favorite_restaurant = models.ForeignKey(Restaurant)
     27
     28    def __unicode__(self):
     29        return self.name
     30
     31__test__ = {'API_TESTS':"""
     32# Regression for #7246
     33
     34>>> r1 = Restaurant.objects.create(name="Nobu", serves_suhsi=True, serves_steak=False)
     35>>> r2 = Restaurant.objects.create(name="Craft", serves_suhsi=False, serves_steak=True)
     36>>> p1 = Person.objects.create(name="John", favorite_restaurant=r1)
     37>>> p2 = Person.objects.create(name="Jane", favorite_restaurant=r2)
     38
     39>>> Person.objects.order_by('name').select_related()
     40[<Person: Jane>, <Person: John>]
     41
     42>>> jane = Person.objects.order_by('name').select_related('favorite_restaurant')[0]
     43>>> jane.favorite_restaurant.name
     44u'Nobu'
     45"""}
     46 No newline at end of file
Back to Top