Ticket #9961: select_related_depth.diff
File select_related_depth.diff, 4.3 KB (added by , 15 years ago) |
---|
-
django/db/models/sql/query.py
1342 1342 (for example, cur_depth=1 means we are looking at models with direct 1343 1343 connections to the root model). 1344 1344 """ 1345 if not restricted and self.max_depth and cur_depth > self.max_depth:1345 if not restricted and self.max_depth is not None and cur_depth > self.max_depth: 1346 1346 # We've recursed far enough; bail out. 1347 1347 return 1348 1348 -
django/db/models/query.py
540 540 If fields are specified, they must be ForeignKey fields and only those 541 541 related objects are included in the selection. 542 542 """ 543 depth = kwargs.pop('depth', 0)543 depth = kwargs.pop('depth', None) 544 544 if kwargs: 545 545 raise TypeError('Unexpected keyword arguments to select_related: %s' 546 546 % (kwargs.keys(),)) … … 551 551 obj.query.add_select_related(fields) 552 552 else: 553 553 obj.query.select_related = True 554 if depth :554 if depth is not None: 555 555 obj.query.max_depth = depth 556 556 return obj 557 557 … … 947 947 value_annotation = False 948 948 949 949 950 def get_cached_row(klass, row, index_start, max_depth= 0, cur_depth=0,950 def get_cached_row(klass, row, index_start, max_depth=None, cur_depth=0, 951 951 requested=None, offset=0, only_load=None): 952 952 """ 953 953 Helper function that recursively returns an object with the specified 954 954 related attributes already populated. 955 955 """ 956 if max_depth and requested is None and cur_depth > max_depth:956 if max_depth is not None and requested is None and cur_depth > max_depth: 957 957 # We've recursed deeply enough; stop now. 958 958 return None 959 959 -
tests/modeltests/select_related/models.py
147 147 >>> len(db.connection.queries) 148 148 5 149 149 150 # If depth=0 then it shouldn't do the select_related 151 >>> db.reset_queries() 152 >>> world = Species.objects.all().select_related(depth=0) 153 >>> [o.genus.family.order for o in world] 154 [<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>] 155 >>> len(db.connection.queries) 156 13 157 158 # If depth=0 then it shouldn't do the select_related 159 >>> db.reset_queries() 160 >>> world = Species.objects.all().select_related('genus').select_related(depth=0) 161 >>> [o.genus.family.order for o in world] 162 [<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>] 163 >>> len(db.connection.queries) 164 13 165 166 # If depth=0 then it shouldn't do the select_related even if a previous select 167 # related was set on the query 168 >>> db.reset_queries() 169 >>> world = Species.objects.all().select_related('genus').select_related(depth=0) 170 >>> [o.genus.family.order for o in world] 171 [<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>] 172 >>> len(db.connection.queries) 173 13 174 175 150 176 >>> s = Species.objects.all().select_related(depth=1).extra(select={'a': 'select_related_species.id + 10'})[0] 151 177 >>> s.id + 10 == s.a 152 178 True -
docs/ref/models/querysets.txt
577 577 p = b.author # Doesn't hit the database. 578 578 c = p.hometown # Requires a database call. 579 579 580 You can also use the ``depth`` argument to cancel an existing 581 ``select_related()`` on the query by setting ``depth`` to "0". For example 582 these three queries are equivalent:: 583 584 b = Book.objects.get(id=4) 585 b = Book.objects.select_related(depth=0).get(id=4) 586 b = Book.objects.select_related('author').select_related(depth=0).get(id=4) 587 580 588 Sometimes you only want to access specific models that are related to your root 581 589 model, not all of the related models. In these cases, you can pass the related 582 590 field names to ``select_related()`` and it will only follow those relations.