Ticket #4789: patch.diff
File patch.diff, 4.6 KB (added by , 17 years ago) |
---|
-
django/db/models/query.py
835 835 """ 836 836 837 837 # 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: 839 839 return None 840 840 841 841 qn = backend.quote_name -
tests/modeltests/select_related/models.py
52 52 def __unicode__(self): 53 53 return self.name 54 54 55 class Color(models.Model): 56 name = models.CharField(maxlength=50) 57 def __unicode__(self): 58 return self.name 59 55 60 class Species(models.Model): 56 61 name = models.CharField(maxlength=50) 57 62 genus = models.ForeignKey(Genus) 63 color = models.ForeignKey(Color) 58 64 def __unicode__(self): 59 65 return self.name 60 66 61 def create_tree(stringtree ):67 def create_tree(stringtree, color_name): 62 68 """Helper to create a complete tree""" 63 69 names = stringtree.split() 64 70 models = [Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species] … … 72 78 obj = model(name=name) 73 79 if parent: 74 80 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 75 87 obj.save() 76 88 parent = obj 77 89 90 91 78 92 __test__ = {'API_TESTS':""" 79 93 80 94 # Set up. … … 83 97 >>> from django.conf import settings 84 98 >>> settings.DEBUG = True 85 99 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") 90 104 91 105 >>> from django import db 92 106 … … 95 109 >>> fly = Species.objects.get(name="melanogaster") 96 110 >>> fly.genus.family.order.klass.phylum.kingdom.domain 97 111 <Domain: Eukaryota> 112 >>> fly.color 113 <Color: cyan> 98 114 >>> len(db.connection.queries) 99 8 115 9 100 116 101 117 # However, a select_related() call will fill in those related objects without any extra queries: 102 118 >>> db.reset_queries() 103 119 >>> person = Species.objects.select_related().get(name="sapiens") 104 120 >>> person.genus.family.order.klass.phylum.kingdom.domain 105 121 <Domain: Eukaryota> 122 >>> person.color 123 <Color: magenta> 106 124 >>> len(db.connection.queries) 107 125 1 108 126 … … 112 130 >>> world = Species.objects.all() 113 131 >>> [o.genus.family for o in world] 114 132 [<Family: Drosophilidae>, <Family: Hominidae>, <Family: Fabaceae>, <Family: Amanitacae>] 133 >>> [o.color for o in world] 134 [<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>] 115 135 >>> len(db.connection.queries) 116 9 136 13 117 137 118 138 # With select_related(): 119 139 >>> db.reset_queries() 120 140 >>> world = Species.objects.all().select_related() 121 141 >>> [o.genus.family for o in world] 122 142 [<Family: Drosophilidae>, <Family: Hominidae>, <Family: Fabaceae>, <Family: Amanitacae>] 143 >>> [o.color for o in world] 144 [<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>] 123 145 >>> len(db.connection.queries) 124 146 1 125 147 … … 128 150 >>> pea = Species.objects.select_related(depth=1).get(name="sativum") 129 151 >>> pea.genus.family.order.klass.phylum.kingdom.domain 130 152 <Domain: Eukaryota> 153 >>> pea.color 154 <Color: yellow> 131 155 132 156 # Notice: one few query than above because of depth=1 133 157 >>> len(db.connection.queries) … … 137 161 >>> pea = Species.objects.select_related(depth=5).get(name="sativum") 138 162 >>> pea.genus.family.order.klass.phylum.kingdom.domain 139 163 <Domain: Eukaryota> 164 >>> pea.color 165 <Color: yellow> 140 166 >>> len(db.connection.queries) 141 167 3 142 168 … … 144 170 >>> world = Species.objects.all().select_related(depth=2) 145 171 >>> [o.genus.family.order for o in world] 146 172 [<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>] 173 >>> [o.color for o in world] 174 [<Color: cyan>, <Color: magenta>, <Color: yellow>, <Color: black>] 147 175 >>> len(db.connection.queries) 148 176 5 149 177