Ticket #7246: 7246.diff

File 7246.diff, 2.7 KB (added by Ivan Sagalaev <Maniac@…>, 16 years ago)

Patch

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

     
    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 model:
     
    894894                            promote=f.null)
    895895            else:
    896896                alias = root_alias
     897
     898            # For parent links all tables are already joined in "if model:" clause above
     899            # Also we don't need to recurse further along parent links
     900            if f.rel.parent_link:
     901                continue
     902
    897903            alias = self.join((alias, table, f.column,
    898904                    f.rel.get_related_field().column), exclusions=used,
    899905                    promote=f.null)
    900906            used.add(alias)
    901907            self.related_select_cols.extend([(alias, f2.column)
    902                     for f2 in f.rel.to._meta.fields])
    903             self.related_select_fields.extend(f.rel.to._meta.fields)
     908                    for f2 in f.rel.to._meta.local_fields])
     909            self.related_select_fields.extend(f.rel.to._meta.local_fields)
    904910            if restricted:
    905911                next = requested.get(f.name, {})
    906912            else:
  • tests/modeltests/model_inheritance/models.py

     
    7979    def __unicode__(self):
    8080        return u"%s the italian restaurant" % self.name
    8181
     82class Pizza(models.Model):
     83    restaurant = models.ForeignKey(ItalianRestaurant)
     84    name = models.CharField(max_length=50)
     85   
     86    def __unicode__(self):
     87        return u"%s at %s" % (self.name, self.restaurant)
     88
    8289class Supplier(Place):
    8390    customers = models.ManyToManyField(Restaurant, related_name='provider')
    8491
     
    263270<Chef: Albert the chef>
    264271>>> len(db.connection.queries)
    2652723
     273>>> restaurant = ItalianRestaurant.objects.all()[0]
     274>>> pizza1 = Pizza.objects.create(name='Margherita', restaurant=restaurant)
     275>>> pizza2 = Pizza.objects.create(name='Capricciosa', restaurant=restaurant)
     276>>> len(Pizza.objects.all().select_related())
     2772
     278>>> len(Pizza.objects.all().select_related('restaurant'))
     2792
     280>>> Pizza.objects.all().select_related()[0]
     281<Pizza: Capricciosa at True the italian restaurant>
    266282>>> settings.DEBUG = False
    267283
    268284"""}
Back to Top