Commit b84f5ab4 authored by chenesan's avatar chenesan Committed by Tim Graham
Browse files

Fixed #26230 -- Made default_related_name affect related_query_name.

parent 5fb9756e
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -290,8 +290,13 @@ class RelatedField(Field):

        if not cls._meta.abstract:
            if self.remote_field.related_name:
                related_name = force_text(self.remote_field.related_name) % {
                related_name = self.remote_field.related_name
            else:
                related_name = self.opts.default_related_name
            if related_name:
                related_name = force_text(related_name) % {
                    'class': cls.__name__.lower(),
                    'model_name': cls._meta.model_name.lower(),
                    'app_label': cls._meta.app_label.lower()
                }
                self.remote_field.related_name = related_name
+0 −5
Original line number Diff line number Diff line
@@ -187,11 +187,6 @@ class ForeignObjectRel(object):
                return None
        if self.related_name:
            return self.related_name
        if opts.default_related_name:
            return opts.default_related_name % {
                'model_name': opts.model_name.lower(),
                'app_label': opts.app_label.lower(),
            }
        return opts.model_name + ('_set' if self.multiple else '')

    def get_cache_name(self):
+15 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ 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
@@ -30,6 +31,7 @@ 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

@@ -1288,6 +1290,19 @@ 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
+3 −0
Original line number Diff line number Diff line
@@ -138,6 +138,9 @@ details on these changes.
* Support for the ``django.core.files.storage.Storage.accessed_time()``,
  ``created_time()``, and ``modified_time()`` methods will be removed.

* Support for query lookups using the model name when
  ``Meta.default_related_name`` is set will be removed.

.. _deprecation-removed-in-1.10:

1.10
+3 −2
Original line number Diff line number Diff line
@@ -1333,8 +1333,9 @@ The possible values for :attr:`~ForeignKey.on_delete` are found in

.. attribute:: ForeignKey.related_query_name

    The name to use for the reverse filter name from the target model.
    Defaults to the value of :attr:`related_name` if it is set, otherwise it
    The name to use for the reverse filter name from the target model. It
    defaults to the value of :attr:`related_name` or
    :attr:`~django.db.models.Options.default_related_name` if set, otherwise it
    defaults to the name of the model::

        # Declare the ForeignKey with related_query_name
Loading