diff --git a/django/db/models/query.py b/django/db/models/query.py
index be42d02..7875eae 100644
a
|
b
|
class QuerySet(object):
|
657 | 657 | If fields are specified, they must be ForeignKey fields and only those |
658 | 658 | related objects are included in the selection. |
659 | 659 | """ |
660 | | depth = kwargs.pop('depth', 0) |
| 660 | depth = kwargs.pop('depth', None) |
661 | 661 | if kwargs: |
662 | 662 | raise TypeError('Unexpected keyword arguments to select_related: %s' |
663 | 663 | % (kwargs.keys(),)) |
… |
… |
class QuerySet(object):
|
668 | 668 | obj.query.add_select_related(fields) |
669 | 669 | else: |
670 | 670 | obj.query.select_related = True |
671 | | if depth: |
| 671 | if depth is not None: |
672 | 672 | obj.query.max_depth = depth |
673 | 673 | return obj |
674 | 674 | |
… |
… |
class EmptyQuerySet(QuerySet):
|
1217 | 1217 | # situations). |
1218 | 1218 | value_annotation = False |
1219 | 1219 | |
1220 | | def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None, |
| 1220 | def get_klass_info(klass, max_depth=None, cur_depth=0, requested=None, |
1221 | 1221 | only_load=None, local_only=False): |
1222 | 1222 | """ |
1223 | 1223 | Helper function that recursively returns an information for a klass, to be |
… |
… |
def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
1241 | 1241 | * local_only - Only populate local fields. This is used when |
1242 | 1242 | following reverse select-related relations |
1243 | 1243 | """ |
1244 | | if max_depth and requested is None and cur_depth > max_depth: |
| 1244 | if max_depth is not None and requested is None and cur_depth > max_depth: |
1245 | 1245 | # We've recursed deeply enough; stop now. |
1246 | 1246 | return None |
1247 | 1247 | |
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index cebd77f..9c63062 100644
a
|
b
|
class SQLCompiler(object):
|
517 | 517 | (for example, cur_depth=1 means we are looking at models with direct |
518 | 518 | connections to the root model). |
519 | 519 | """ |
520 | | if not restricted and self.query.max_depth and cur_depth > self.query.max_depth: |
| 520 | if not restricted and self.query.max_depth is not None and cur_depth > self.query.max_depth: |
521 | 521 | # We've recursed far enough; bail out. |
522 | 522 | return |
523 | 523 | |
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index 68da9c7..9cae243 100644
a
|
b
|
follow::
|
642 | 642 | p = b.author # Doesn't hit the database. |
643 | 643 | c = p.hometown # Requires a database call. |
644 | 644 | |
| 645 | You can also use the ``depth`` argument to cancel an existing |
| 646 | ``select_related()`` on the query by setting ``depth`` to "0". For example |
| 647 | these three queries are equivalent:: |
| 648 | |
| 649 | b = Book.objects.get(id=4) |
| 650 | b = Book.objects.select_related(depth=0).get(id=4) |
| 651 | b = Book.objects.select_related('author').select_related(depth=0).get(id=4) |
| 652 | |
645 | 653 | Sometimes you only want to access specific models that are related to your root |
646 | 654 | model, not all of the related models. In these cases, you can pass the related |
647 | 655 | field names to ``select_related()`` and it will only follow those relations. |
diff --git a/tests/modeltests/select_related/tests.py b/tests/modeltests/select_related/tests.py
index 1b3715a..a6641db 100644
a
|
b
|
class SelectRelatedTests(TestCase):
|
160 | 160 | Species.objects.select_related, |
161 | 161 | 'genus__family__order', depth=4 |
162 | 162 | ) |
| 163 | |
| 164 | def test_depth_zero(self): |
| 165 | with self.assertNumQueries(9): |
| 166 | world = Species.objects.all().select_related(depth=0) |
| 167 | families = [o.genus.family.name for o in world] |
| 168 | self.assertEqual(sorted(families), [ |
| 169 | 'Amanitacae', |
| 170 | 'Drosophilidae', |
| 171 | 'Fabaceae', |
| 172 | 'Hominidae', |
| 173 | ]) |
| 174 | |
| 175 | def test_reset_depth(self): |
| 176 | with self.assertNumQueries(9): |
| 177 | world = Species.objects.all().select_related('genus').select_related(depth=0) |
| 178 | families = [o.genus.family.name for o in world] |
| 179 | self.assertEqual(sorted(families), [ |
| 180 | 'Amanitacae', |
| 181 | 'Drosophilidae', |
| 182 | 'Fabaceae', |
| 183 | 'Hominidae', |
| 184 | ]) |
| 185 | |