Refs #26230 -- Removed support for model name query lookups when using Meta.default_related_name.
Per deprecation timeline.
This commit is contained in:
parent
2d7fb77987
commit
bfe0d54514
|
@ -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.
|
||||
"""
|
||||
import copy
|
||||
import warnings
|
||||
from collections import Counter, Iterator, Mapping, OrderedDict
|
||||
from itertools import chain, count, product
|
||||
from string import ascii_uppercase
|
||||
|
@ -32,7 +31,6 @@ from django.db.models.sql.where import (
|
|||
AND, OR, ExtraWhere, NothingNode, WhereNode,
|
||||
)
|
||||
from django.utils import six
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.tree import Node
|
||||
|
||||
|
@ -1320,19 +1318,6 @@ class Query(object):
|
|||
except FieldDoesNotExist:
|
||||
if name in self.annotation_select:
|
||||
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:
|
||||
# Fields that contain one-to-many relations with a generic
|
||||
|
|
|
@ -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
|
||||
: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``
|
||||
-----------------
|
||||
|
||||
|
|
|
@ -350,3 +350,6 @@ these features.
|
|||
|
||||
* Support for the ``django.core.files.storage.Storage.accessed_time()``,
|
||||
``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.
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import warnings
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.test import TestCase
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
|
||||
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):
|
||||
self.assertEqual(Author.objects.get(books=self.book), self.author)
|
||||
|
||||
def test_show_deprecated_message_when_model_name_in_queryset_lookup(self):
|
||||
msg = "Query lookup 'book' is deprecated in favor of Meta.default_related_name 'books'."
|
||||
with warnings.catch_warnings(record=True) as warns:
|
||||
warnings.simplefilter('once')
|
||||
def test_model_name_not_available_in_queryset_lookup(self):
|
||||
msg = "Cannot resolve keyword 'book' into field."
|
||||
with self.assertRaisesMessage(FieldError, msg):
|
||||
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):
|
||||
self.assertEqual(list(self.editor.edited_books.all()), [self.book])
|
||||
|
|
Loading…
Reference in New Issue