Ticket #4789: patch.diff

File patch.diff, 4.6 KB (added by Gábor Farkas <gabor@…>, 17 years ago)
  • django/db/models/query.py

     
    835835    """
    836836
    837837    # If we've got a max_depth set and we've exceeded that depth, bail now.
    838     if max_depth and cur_depth > max_depth:
     838    if max_depth and cur_depth >= max_depth:
    839839        return None
    840840
    841841    qn = backend.quote_name
  • tests/modeltests/select_related/models.py

     
    5252    def __unicode__(self):
    5353        return self.name
    5454
     55class Color(models.Model):
     56    name = models.CharField(maxlength=50)
     57    def __unicode__(self):
     58        return self.name
     59
    5560class Species(models.Model):
    5661    name = models.CharField(maxlength=50)
    5762    genus = models.ForeignKey(Genus)
     63    color = models.ForeignKey(Color)
    5864    def __unicode__(self):
    5965        return self.name
    6066
    61 def create_tree(stringtree):
     67def create_tree(stringtree, color_name):
    6268    """Helper to create a complete tree"""
    6369    names = stringtree.split()
    6470    models = [Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species]
     
    7278            obj = model(name=name)
    7379        if parent:
    7480            setattr(obj, parent.__class__.__name__.lower(), parent)
     81
     82        if model == Species:
     83            color = Color(name=color_name)
     84            color.save()
     85            obj.color = color
     86
    7587        obj.save()
    7688        parent = obj
    7789
     90
     91
    7892__test__ = {'API_TESTS':"""
    7993
    8094# Set up.
     
    8397>>> from django.conf import settings
    8498>>> settings.DEBUG = True
    8599
    86 >>> create_tree("Eukaryota Animalia Anthropoda Insecta Diptera Drosophilidae Drosophila melanogaster")
    87 >>> create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens")
    88 >>> create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum")
    89 >>> create_tree("Eukaryota Fungi Basidiomycota Homobasidiomycatae Agaricales Amanitacae Amanita muscaria")
     100>>> create_tree("Eukaryota Animalia Anthropoda Insecta Diptera Drosophilidae Drosophila melanogaster","cyan")
     101>>> create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens","magenta")
     102>>> create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum","yellow")
     103>>> create_tree("Eukaryota Fungi Basidiomycota Homobasidiomycatae Agaricales Amanitacae Amanita muscaria","black")
    90104
    91105>>> from django import db
    92106
     
    95109>>> fly = Species.objects.get(name="melanogaster")
    96110>>> fly.genus.family.order.klass.phylum.kingdom.domain
    97111<Domain: Eukaryota>
     112>>> fly.color
     113<Color: cyan>
    98114>>> len(db.connection.queries)
    99 8
     1159
    100116
    101117# However, a select_related() call will fill in those related objects without any extra queries:
    102118>>> db.reset_queries()
    103119>>> person = Species.objects.select_related().get(name="sapiens")
    104120>>> person.genus.family.order.klass.phylum.kingdom.domain
    105121<Domain: Eukaryota>
     122>>> person.color
     123<Color: magenta>
    106124>>> len(db.connection.queries)
    1071251
    108126
     
    112130>>> world = Species.objects.all()
    113131>>> [o.genus.family for o in world]
    114132[<Family: Drosophilidae>, <Family: Hominidae>, <Family: Fabaceae>, <Family: Amanitacae>]
     133>>> [o.color for o in world]
     134[<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>]
    115135>>> len(db.connection.queries)
    116 9
     13613
    117137
    118138# With select_related():
    119139>>> db.reset_queries()
    120140>>> world = Species.objects.all().select_related()
    121141>>> [o.genus.family for o in world]
    122142[<Family: Drosophilidae>, <Family: Hominidae>, <Family: Fabaceae>, <Family: Amanitacae>]
     143>>> [o.color for o in world]
     144[<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>]
    123145>>> len(db.connection.queries)
    1241461
    125147
     
    128150>>> pea = Species.objects.select_related(depth=1).get(name="sativum")
    129151>>> pea.genus.family.order.klass.phylum.kingdom.domain
    130152<Domain: Eukaryota>
     153>>> pea.color
     154<Color: yellow>
    131155
    132156# Notice: one few query than above because of depth=1
    133157>>> len(db.connection.queries)
     
    137161>>> pea = Species.objects.select_related(depth=5).get(name="sativum")
    138162>>> pea.genus.family.order.klass.phylum.kingdom.domain
    139163<Domain: Eukaryota>
     164>>> pea.color
     165<Color: yellow>
    140166>>> len(db.connection.queries)
    1411673
    142168
     
    144170>>> world = Species.objects.all().select_related(depth=2)
    145171>>> [o.genus.family.order for o in world]
    146172[<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>]
     173>>> [o.color for o in world]
     174[<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>]
    147175>>> len(db.connection.queries)
    1481765
    149177
Back to Top