Removed 'depth' .select_related() argument as per deprecation TL.

This commit is contained in:
Ramiro Morales 2013-06-28 20:00:50 -03:00
parent c196564132
commit 6ba69c8456
3 changed files with 13 additions and 70 deletions

View File

@ -5,7 +5,6 @@ The main QuerySet implementation. This provides the public API for the ORM.
import copy import copy
import itertools import itertools
import sys import sys
import warnings
from django.conf import settings from django.conf import settings
from django.core import exceptions from django.core import exceptions
@ -648,10 +647,6 @@ class QuerySet(object):
If select_related(None) is called, the list is cleared. If select_related(None) is called, the list is cleared.
""" """
if 'depth' in kwargs:
warnings.warn('The "depth" keyword argument has been deprecated.\n'
'Use related field names instead.', DeprecationWarning, stacklevel=2)
depth = kwargs.pop('depth', 0)
if kwargs: if kwargs:
raise TypeError('Unexpected keyword arguments to select_related: %s' raise TypeError('Unexpected keyword arguments to select_related: %s'
% (list(kwargs),)) % (list(kwargs),))
@ -659,13 +654,9 @@ class QuerySet(object):
if fields == (None,): if fields == (None,):
obj.query.select_related = False obj.query.select_related = False
elif fields: elif fields:
if depth:
raise TypeError('Cannot pass both "depth" and fields to select_related()')
obj.query.add_select_related(fields) obj.query.add_select_related(fields)
else: else:
obj.query.select_related = True obj.query.select_related = True
if depth:
obj.query.max_depth = depth
return obj return obj
def prefetch_related(self, *lookups): def prefetch_related(self, *lookups):

View File

@ -764,8 +764,6 @@ You can refer to any :class:`~django.db.models.ForeignKey` or
:class:`~django.db.models.OneToOneField` relation in the list of fields :class:`~django.db.models.OneToOneField` relation in the list of fields
passed to ``select_related()``. This includes foreign keys that have passed to ``select_related()``. This includes foreign keys that have
``null=True`` (which are omitted in a no-parameter ``select_related()`` call). ``null=True`` (which are omitted in a no-parameter ``select_related()`` call).
It's an error to use both a list of fields and the ``depth`` parameter in the
same ``select_related()`` call; they are conflicting options.
You can also refer to the reverse direction of a You can also refer to the reverse direction of a
:class:`~django.db.models.OneToOneField` in the list of fields passed to :class:`~django.db.models.OneToOneField` in the list of fields passed to
@ -781,20 +779,6 @@ If you need to clear the list of related fields added by past calls of
>>> without_relations = queryset.select_related(None) >>> without_relations = queryset.select_related(None)
.. deprecated:: 1.5
The ``depth`` parameter to ``select_related()`` has been deprecated. You
should replace it with the use of the ``(*fields)`` listing specific
related fields instead as documented above.
A depth limit of relationships to follow can also be specified::
b = Book.objects.select_related(depth=1).get(id=4)
p = b.author # Doesn't hit the database.
c = p.hometown # Requires a database call.
A :class:`~django.db.models.OneToOneField` is not traversed in the reverse
direction if you are performing a depth-based ``select_related()`` call.
prefetch_related prefetch_related
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

View File

@ -1,7 +1,5 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import warnings
from django.test import TestCase from django.test import TestCase
from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
@ -55,9 +53,7 @@ class SelectRelatedTests(TestCase):
extra queries extra queries
""" """
with self.assertNumQueries(1): with self.assertNumQueries(1):
with warnings.catch_warnings(): person = Species.objects.select_related('genus__family__order__klass__phylum__kingdom__domain').get(name="sapiens")
warnings.simplefilter("ignore", DeprecationWarning)
person = Species.objects.select_related(depth=10).get(name="sapiens")
domain = person.genus.family.order.klass.phylum.kingdom.domain domain = person.genus.family.order.klass.phylum.kingdom.domain
self.assertEqual(domain.name, 'Eukaryota') self.assertEqual(domain.name, 'Eukaryota')
@ -91,53 +87,27 @@ class SelectRelatedTests(TestCase):
'Hominidae', 'Hominidae',
]) ])
def test_depth(self, depth=1, expected=7):
"""
The "depth" argument to select_related() will stop the descent at a
particular level.
"""
# Notice: one fewer queries than above because of depth=1
with self.assertNumQueries(expected):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
pea = Species.objects.select_related(depth=depth).get(name="sativum")
self.assertEqual(
pea.genus.family.order.klass.phylum.kingdom.domain.name,
'Eukaryota'
)
def test_larger_depth(self):
"""
The "depth" argument to select_related() will stop the descent at a
particular level. This tests a larger depth value.
"""
self.test_depth(depth=5, expected=3)
def test_list_with_depth(self): def test_list_with_depth(self):
""" """
The "depth" argument to select_related() will stop the descent at a Passing a relationship field lookup specifier to select_related() will
particular level. This can be used on lists as well. stop the descent at a particular level. This can be used on lists as
well.
""" """
with self.assertNumQueries(5): with self.assertNumQueries(5):
with warnings.catch_warnings(): world = Species.objects.all().select_related('genus__family')
warnings.simplefilter("ignore", DeprecationWarning)
world = Species.objects.all().select_related(depth=2)
orders = [o.genus.family.order.name for o in world] orders = [o.genus.family.order.name for o in world]
self.assertEqual(sorted(orders), self.assertEqual(sorted(orders),
['Agaricales', 'Diptera', 'Fabales', 'Primates']) ['Agaricales', 'Diptera', 'Fabales', 'Primates'])
def test_select_related_with_extra(self): def test_select_related_with_extra(self):
with warnings.catch_warnings(): s = Species.objects.all().select_related()\
warnings.simplefilter("ignore", DeprecationWarning)
s = Species.objects.all().select_related(depth=1)\
.extra(select={'a': 'select_related_species.id + 10'})[0] .extra(select={'a': 'select_related_species.id + 10'})[0]
self.assertEqual(s.id + 10, s.a) self.assertEqual(s.id + 10, s.a)
def test_certain_fields(self): def test_certain_fields(self):
""" """
The optional fields passed to select_related() control which related The optional fields passed to select_related() control which related
models we pull in. This allows for smaller queries and can act as an models we pull in. This allows for smaller queries.
alternative (or, in addition to) the depth parameter.
In this case, we explicitly say to select the 'genus' and In this case, we explicitly say to select the 'genus' and
'genus.family' models, leading to the same number of queries as before. 'genus.family' models, leading to the same number of queries as before.
@ -166,8 +136,6 @@ class SelectRelatedTests(TestCase):
self.assertEqual(s, 'Diptera') self.assertEqual(s, 'Diptera')
def test_depth_fields_fails(self): def test_depth_fields_fails(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertRaises(TypeError, self.assertRaises(TypeError,
Species.objects.select_related, Species.objects.select_related,
'genus__family__order', depth=4 'genus__family__order', depth=4