Refs #32682 -- Renamed lookup_needs_distinct() to lookup_spawns_duplicates().
Follow up to 1871182031
.
This commit is contained in:
parent
4f128fcf5d
commit
baba733dcc
|
@ -17,7 +17,7 @@ from django.contrib.admin.exceptions import DisallowedModelAdminToField
|
||||||
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
||||||
from django.contrib.admin.utils import (
|
from django.contrib.admin.utils import (
|
||||||
NestedObjects, construct_change_message, flatten_fieldsets,
|
NestedObjects, construct_change_message, flatten_fieldsets,
|
||||||
get_deleted_objects, lookup_needs_distinct, model_format_dict,
|
get_deleted_objects, lookup_spawns_duplicates, model_format_dict,
|
||||||
model_ngettext, quote, unquote,
|
model_ngettext, quote, unquote,
|
||||||
)
|
)
|
||||||
from django.contrib.admin.widgets import (
|
from django.contrib.admin.widgets import (
|
||||||
|
@ -1031,7 +1031,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
for orm_lookup in orm_lookups]
|
for orm_lookup in orm_lookups]
|
||||||
queryset = queryset.filter(reduce(operator.or_, or_queries))
|
queryset = queryset.filter(reduce(operator.or_, or_queries))
|
||||||
may_have_duplicates |= any(
|
may_have_duplicates |= any(
|
||||||
lookup_needs_distinct(self.opts, search_spec)
|
lookup_spawns_duplicates(self.opts, search_spec)
|
||||||
for search_spec in orm_lookups
|
for search_spec in orm_lookups
|
||||||
)
|
)
|
||||||
return queryset, may_have_duplicates
|
return queryset, may_have_duplicates
|
||||||
|
|
|
@ -25,9 +25,9 @@ class FieldIsAForeignKeyColumnName(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def lookup_needs_distinct(opts, lookup_path):
|
def lookup_spawns_duplicates(opts, lookup_path):
|
||||||
"""
|
"""
|
||||||
Return True if 'distinct()' should be used to query the given lookup path.
|
Return True if the given lookup path spawns duplicates.
|
||||||
"""
|
"""
|
||||||
lookup_fields = lookup_path.split(LOOKUP_SEP)
|
lookup_fields = lookup_path.split(LOOKUP_SEP)
|
||||||
# Go through the fields (following all relations) and look for an m2m.
|
# Go through the fields (following all relations) and look for an m2m.
|
||||||
|
@ -45,7 +45,8 @@ def lookup_needs_distinct(opts, lookup_path):
|
||||||
path_info = field.get_path_info()
|
path_info = field.get_path_info()
|
||||||
opts = path_info[-1].to_opts
|
opts = path_info[-1].to_opts
|
||||||
if any(path.m2m for path in path_info):
|
if any(path.m2m for path in path_info):
|
||||||
# This field is a m2m relation so distinct must be called.
|
# This field is a m2m relation so duplicates must be
|
||||||
|
# handled.
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,8 @@ from django.contrib.admin.options import (
|
||||||
IS_POPUP_VAR, TO_FIELD_VAR, IncorrectLookupParameters,
|
IS_POPUP_VAR, TO_FIELD_VAR, IncorrectLookupParameters,
|
||||||
)
|
)
|
||||||
from django.contrib.admin.utils import (
|
from django.contrib.admin.utils import (
|
||||||
get_fields_from_path, lookup_needs_distinct, prepare_lookup_value, quote,
|
get_fields_from_path, lookup_spawns_duplicates, prepare_lookup_value,
|
||||||
|
quote,
|
||||||
)
|
)
|
||||||
from django.core.exceptions import (
|
from django.core.exceptions import (
|
||||||
FieldDoesNotExist, ImproperlyConfigured, SuspiciousOperation,
|
FieldDoesNotExist, ImproperlyConfigured, SuspiciousOperation,
|
||||||
|
@ -154,10 +155,12 @@ class ChangeList:
|
||||||
self.model, self.model_admin, field_path=field_path,
|
self.model, self.model_admin, field_path=field_path,
|
||||||
)
|
)
|
||||||
# field_list_filter_class removes any lookup_params it
|
# field_list_filter_class removes any lookup_params it
|
||||||
# processes. If that happened, check if distinct() is needed to
|
# processes. If that happened, check if duplicates should be
|
||||||
# remove duplicate results.
|
# removed.
|
||||||
if lookup_params_count > len(lookup_params):
|
if lookup_params_count > len(lookup_params):
|
||||||
may_have_duplicates |= lookup_needs_distinct(self.lookup_opts, field_path)
|
may_have_duplicates |= lookup_spawns_duplicates(
|
||||||
|
self.lookup_opts, field_path,
|
||||||
|
)
|
||||||
if spec and spec.has_output():
|
if spec and spec.has_output():
|
||||||
filter_specs.append(spec)
|
filter_specs.append(spec)
|
||||||
if lookup_params_count > len(lookup_params):
|
if lookup_params_count > len(lookup_params):
|
||||||
|
@ -198,12 +201,12 @@ class ChangeList:
|
||||||
# have been removed from lookup_params, which now only contains other
|
# have been removed from lookup_params, which now only contains other
|
||||||
# parameters passed via the query string. We now loop through the
|
# parameters passed via the query string. We now loop through the
|
||||||
# remaining parameters both to ensure that all the parameters are valid
|
# remaining parameters both to ensure that all the parameters are valid
|
||||||
# fields and to determine if at least one of them needs distinct(). If
|
# fields and to determine if at least one of them spawns duplicates. If
|
||||||
# the lookup parameters aren't real fields, then bail out.
|
# the lookup parameters aren't real fields, then bail out.
|
||||||
try:
|
try:
|
||||||
for key, value in lookup_params.items():
|
for key, value in lookup_params.items():
|
||||||
lookup_params[key] = prepare_lookup_value(key, value)
|
lookup_params[key] = prepare_lookup_value(key, value)
|
||||||
may_have_duplicates |= lookup_needs_distinct(self.lookup_opts, key)
|
may_have_duplicates |= lookup_spawns_duplicates(self.lookup_opts, key)
|
||||||
return (
|
return (
|
||||||
filter_specs, bool(filter_specs), lookup_params, may_have_duplicates,
|
filter_specs, bool(filter_specs), lookup_params, may_have_duplicates,
|
||||||
has_active_filters,
|
has_active_filters,
|
||||||
|
|
|
@ -396,6 +396,9 @@ Miscellaneous
|
||||||
As a side-effect ``makemigrations`` might generate no-op ``AlterField``
|
As a side-effect ``makemigrations`` might generate no-op ``AlterField``
|
||||||
operations for ``ForeignKey`` fields in some cases.
|
operations for ``ForeignKey`` fields in some cases.
|
||||||
|
|
||||||
|
* The undocumented ``django.contrib.admin.utils.lookup_needs_distinct()``
|
||||||
|
function is renamed to ``lookup_spawns_duplicates()``.
|
||||||
|
|
||||||
.. _deprecated-features-4.0:
|
.. _deprecated-features-4.0:
|
||||||
|
|
||||||
Features deprecated in 4.0
|
Features deprecated in 4.0
|
||||||
|
|
Loading…
Reference in New Issue