Ticket #9961: 9961_r17068.diff

File 9961_r17068.diff, 4.3 KB (added by Koen Biermans, 13 years ago)

updated the patch to r17068

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index be42d02..7875eae 100644
    a b class QuerySet(object):  
    657657        If fields are specified, they must be ForeignKey fields and only those
    658658        related objects are included in the selection.
    659659        """
    660         depth = kwargs.pop('depth', 0)
     660        depth = kwargs.pop('depth', None)
    661661        if kwargs:
    662662            raise TypeError('Unexpected keyword arguments to select_related: %s'
    663663                    % (kwargs.keys(),))
    class QuerySet(object):  
    668668            obj.query.add_select_related(fields)
    669669        else:
    670670            obj.query.select_related = True
    671         if depth:
     671        if depth is not None:
    672672            obj.query.max_depth = depth
    673673        return obj
    674674
    class EmptyQuerySet(QuerySet):  
    12171217    # situations).
    12181218    value_annotation = False
    12191219
    1220 def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
     1220def get_klass_info(klass, max_depth=None, cur_depth=0, requested=None,
    12211221                   only_load=None, local_only=False):
    12221222    """
    12231223    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,  
    12411241     * local_only - Only populate local fields. This is used when
    12421242       following reverse select-related relations
    12431243    """
    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:
    12451245        # We've recursed deeply enough; stop now.
    12461246        return None
    12471247
  • django/db/models/sql/compiler.py

    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):  
    517517        (for example, cur_depth=1 means we are looking at models with direct
    518518        connections to the root model).
    519519        """
    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:
    521521            # We've recursed far enough; bail out.
    522522            return
    523523
  • docs/ref/models/querysets.txt

    diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
    index 68da9c7..9cae243 100644
    a b follow::  
    642642    p = b.author         # Doesn't hit the database.
    643643    c = p.hometown       # Requires a database call.
    644644
     645You can also use the ``depth`` argument to cancel an existing
     646``select_related()`` on the query by setting ``depth`` to "0". For example
     647these 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
    645653Sometimes you only want to access specific models that are related to your root
    646654model, not all of the related models. In these cases, you can pass the related
    647655field names to ``select_related()`` and it will only follow those relations.
  • tests/modeltests/select_related/tests.py

    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):  
    160160            Species.objects.select_related,
    161161            'genus__family__order', depth=4
    162162        )
     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
Back to Top