Refs #26230 -- Removed support for model name query lookups when using Meta.default_related_name.

Per deprecation timeline.
This commit is contained in:
Tim Graham 2016-12-31 11:24:40 -05:00
parent 2d7fb77987
commit bfe0d54514
4 changed files with 7 additions and 50 deletions

View File

@ -7,7 +7,6 @@ databases). The abstraction barrier only works one way: this module has to know
all about the internals of models in order to get the information it needs. all about the internals of models in order to get the information it needs.
""" """
import copy import copy
import warnings
from collections import Counter, Iterator, Mapping, OrderedDict from collections import Counter, Iterator, Mapping, OrderedDict
from itertools import chain, count, product from itertools import chain, count, product
from string import ascii_uppercase from string import ascii_uppercase
@ -32,7 +31,6 @@ from django.db.models.sql.where import (
AND, OR, ExtraWhere, NothingNode, WhereNode, AND, OR, ExtraWhere, NothingNode, WhereNode,
) )
from django.utils import six from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.tree import Node from django.utils.tree import Node
@ -1320,19 +1318,6 @@ class Query(object):
except FieldDoesNotExist: except FieldDoesNotExist:
if name in self.annotation_select: if name in self.annotation_select:
field = self.annotation_select[name].output_field field = self.annotation_select[name].output_field
elif pos == 0:
for rel in opts.related_objects:
if (name == rel.related_model._meta.model_name and
rel.related_name == rel.related_model._meta.default_related_name):
related_name = rel.related_name
field = opts.get_field(related_name)
warnings.warn(
"Query lookup '%s' is deprecated in favor of "
"Meta.default_related_name '%s'."
% (name, related_name),
RemovedInDjango20Warning, 2
)
break
if field is not None: if field is not None:
# Fields that contain one-to-many relations with a generic # Fields that contain one-to-many relations with a generic

View File

@ -130,30 +130,6 @@ Django quotes column and table names behind the scenes.
and the name of the model, both lowercased. See the paragraph on and the name of the model, both lowercased. See the paragraph on
:ref:`related names for abstract models <abstract-related-name>`. :ref:`related names for abstract models <abstract-related-name>`.
.. deprecated:: 1.10
This attribute now affects ``related_query_name``. The old query lookup
name is deprecated::
from django.db import models
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo)
class Meta:
default_related_name = 'bars'
::
>>> bar = Bar.objects.get(pk=1)
>>> # Using model name "bar" as lookup string is deprecated.
>>> Foo.objects.get(bar=bar)
>>> # You should use default_related_name "bars".
>>> Foo.objects.get(bars=bar)
``get_latest_by`` ``get_latest_by``
----------------- -----------------

View File

@ -350,3 +350,6 @@ these features.
* Support for the ``django.core.files.storage.Storage.accessed_time()``, * Support for the ``django.core.files.storage.Storage.accessed_time()``,
``created_time()``, and ``modified_time()`` methods is removed. ``created_time()``, and ``modified_time()`` methods is removed.
* Support for query lookups using the model name when
``Meta.default_related_name`` is set is removed.

View File

@ -1,7 +1,5 @@
import warnings from django.core.exceptions import FieldError
from django.test import TestCase from django.test import TestCase
from django.utils.deprecation import RemovedInDjango20Warning
from .models.default_related_name import Author, Book, Editor from .models.default_related_name import Author, Book, Editor
@ -24,15 +22,10 @@ class DefaultRelatedNameTests(TestCase):
def test_default_related_name_in_queryset_lookup(self): def test_default_related_name_in_queryset_lookup(self):
self.assertEqual(Author.objects.get(books=self.book), self.author) self.assertEqual(Author.objects.get(books=self.book), self.author)
def test_show_deprecated_message_when_model_name_in_queryset_lookup(self): def test_model_name_not_available_in_queryset_lookup(self):
msg = "Query lookup 'book' is deprecated in favor of Meta.default_related_name 'books'." msg = "Cannot resolve keyword 'book' into field."
with warnings.catch_warnings(record=True) as warns: with self.assertRaisesMessage(FieldError, msg):
warnings.simplefilter('once')
Author.objects.get(book=self.book) Author.objects.get(book=self.book)
self.assertEqual(len(warns), 1)
warning = warns.pop()
self.assertEqual(warning.category, RemovedInDjango20Warning)
self.assertEqual(str(warning.message), msg)
def test_related_name_overrides_default_related_name(self): def test_related_name_overrides_default_related_name(self):
self.assertEqual(list(self.editor.edited_books.all()), [self.book]) self.assertEqual(list(self.editor.edited_books.all()), [self.book])