2013-03-19 13:04:59 +08:00
|
|
|
import sys
|
2015-01-28 20:35:27 +08:00
|
|
|
from collections import OrderedDict
|
2011-05-03 18:44:23 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.admin import FieldListFilter
|
|
|
|
from django.contrib.admin.exceptions import (
|
|
|
|
DisallowedModelAdminLookup, DisallowedModelAdminToField,
|
|
|
|
)
|
|
|
|
from django.contrib.admin.options import (
|
|
|
|
IS_POPUP_VAR, TO_FIELD_VAR, IncorrectLookupParameters,
|
|
|
|
)
|
|
|
|
from django.contrib.admin.utils import (
|
|
|
|
get_fields_from_path, lookup_needs_distinct, prepare_lookup_value, quote,
|
|
|
|
)
|
|
|
|
from django.core.exceptions import (
|
|
|
|
FieldDoesNotExist, ImproperlyConfigured, SuspiciousOperation,
|
|
|
|
)
|
2011-03-03 09:54:33 +08:00
|
|
|
from django.core.paginator import InvalidPage
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2013-03-08 22:15:23 +08:00
|
|
|
from django.utils import six
|
2013-12-07 22:52:30 +08:00
|
|
|
from django.utils.encoding import force_text
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.utils.http import urlencode
|
2015-03-12 21:40:03 +08:00
|
|
|
from django.utils.translation import ugettext
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Changelist settings
|
2005-11-26 05:20:09 +08:00
|
|
|
ALL_VAR = 'all'
|
|
|
|
ORDER_VAR = 'o'
|
|
|
|
ORDER_TYPE_VAR = 'ot'
|
|
|
|
PAGE_VAR = 'p'
|
|
|
|
SEARCH_VAR = 'q'
|
2006-05-31 23:25:23 +08:00
|
|
|
ERROR_FLAG = 'e'
|
2005-11-26 05:20:09 +08:00
|
|
|
|
2011-05-03 18:44:23 +08:00
|
|
|
IGNORED_PARAMS = (
|
|
|
|
ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR)
|
|
|
|
|
2011-04-23 11:55:21 +08:00
|
|
|
|
2014-03-21 19:45:38 +08:00
|
|
|
class ChangeList(object):
|
2011-05-03 18:44:23 +08:00
|
|
|
def __init__(self, request, model, list_display, list_display_links,
|
2016-03-29 06:33:29 +08:00
|
|
|
list_filter, date_hierarchy, search_fields, list_select_related,
|
|
|
|
list_per_page, list_max_show_all, list_editable, model_admin):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.model = model
|
|
|
|
self.opts = model._meta
|
|
|
|
self.lookup_opts = self.opts
|
2013-03-08 22:15:23 +08:00
|
|
|
self.root_queryset = model_admin.get_queryset(request)
|
2008-07-19 07:54:34 +08:00
|
|
|
self.list_display = list_display
|
|
|
|
self.list_display_links = list_display_links
|
|
|
|
self.list_filter = list_filter
|
|
|
|
self.date_hierarchy = date_hierarchy
|
|
|
|
self.search_fields = search_fields
|
|
|
|
self.list_select_related = list_select_related
|
|
|
|
self.list_per_page = list_per_page
|
2011-09-08 21:25:00 +08:00
|
|
|
self.list_max_show_all = list_max_show_all
|
2008-07-19 07:54:34 +08:00
|
|
|
self.model_admin = model_admin
|
2013-06-19 03:41:36 +08:00
|
|
|
self.preserved_filters = model_admin.get_preserved_filters(request)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Get search parameters from the query string.
|
|
|
|
try:
|
|
|
|
self.page_num = int(request.GET.get(PAGE_VAR, 0))
|
|
|
|
except ValueError:
|
|
|
|
self.page_num = 0
|
2007-04-26 21:30:48 +08:00
|
|
|
self.show_all = ALL_VAR in request.GET
|
2014-03-21 23:35:33 +08:00
|
|
|
self.is_popup = IS_POPUP_VAR in request.GET
|
2014-08-07 12:18:10 +08:00
|
|
|
to_field = request.GET.get(TO_FIELD_VAR)
|
|
|
|
if to_field and not model_admin.to_field_allowed(request, to_field):
|
|
|
|
raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field)
|
|
|
|
self.to_field = to_field
|
2006-05-02 09:31:56 +08:00
|
|
|
self.params = dict(request.GET.items())
|
2007-04-26 21:30:48 +08:00
|
|
|
if PAGE_VAR in self.params:
|
2006-05-02 09:31:56 +08:00
|
|
|
del self.params[PAGE_VAR]
|
2007-04-26 21:30:48 +08:00
|
|
|
if ERROR_FLAG in self.params:
|
2006-05-31 23:25:23 +08:00
|
|
|
del self.params[ERROR_FLAG]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-01-02 09:32:40 +08:00
|
|
|
if self.is_popup:
|
|
|
|
self.list_editable = ()
|
|
|
|
else:
|
|
|
|
self.list_editable = list_editable
|
2006-05-02 09:31:56 +08:00
|
|
|
self.query = request.GET.get(SEARCH_VAR, '')
|
2013-03-08 22:15:23 +08:00
|
|
|
self.queryset = self.get_queryset(request)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.get_results(request)
|
2011-05-03 18:44:23 +08:00
|
|
|
if self.is_popup:
|
|
|
|
title = ugettext('Select %s')
|
|
|
|
else:
|
|
|
|
title = ugettext('Select %s to change')
|
2012-07-21 16:00:10 +08:00
|
|
|
self.title = title % force_text(self.opts.verbose_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.pk_attname = self.lookup_opts.pk.attname
|
|
|
|
|
2013-02-24 20:18:21 +08:00
|
|
|
def get_filters_params(self, params=None):
|
|
|
|
"""
|
|
|
|
Returns all params except IGNORED_PARAMS
|
|
|
|
"""
|
|
|
|
if not params:
|
|
|
|
params = self.params
|
2013-11-03 05:02:56 +08:00
|
|
|
lookup_params = params.copy() # a dictionary of the query string
|
2011-11-22 20:26:17 +08:00
|
|
|
# Remove all the parameters that are globally and systematically
|
|
|
|
# ignored.
|
|
|
|
for ignored in IGNORED_PARAMS:
|
|
|
|
if ignored in lookup_params:
|
|
|
|
del lookup_params[ignored]
|
2013-02-24 20:18:21 +08:00
|
|
|
return lookup_params
|
|
|
|
|
|
|
|
def get_filters(self, request):
|
|
|
|
lookup_params = self.get_filters_params()
|
|
|
|
use_distinct = False
|
2011-11-22 20:26:17 +08:00
|
|
|
|
|
|
|
for key, value in lookup_params.items():
|
|
|
|
if not self.model_admin.lookup_allowed(key, value):
|
2013-05-16 07:14:28 +08:00
|
|
|
raise DisallowedModelAdminLookup("Filtering by %s not allowed" % key)
|
2011-11-22 20:26:17 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
filter_specs = []
|
2008-08-16 03:15:20 +08:00
|
|
|
if self.list_filter:
|
2011-06-22 20:20:52 +08:00
|
|
|
for list_filter in self.list_filter:
|
|
|
|
if callable(list_filter):
|
2011-05-03 18:44:23 +08:00
|
|
|
# This is simply a custom list filter class.
|
2016-03-29 06:33:29 +08:00
|
|
|
spec = list_filter(request, lookup_params, self.model, self.model_admin)
|
2011-05-03 18:44:23 +08:00
|
|
|
else:
|
|
|
|
field_path = None
|
2011-06-22 20:20:52 +08:00
|
|
|
if isinstance(list_filter, (tuple, list)):
|
2011-05-24 17:17:55 +08:00
|
|
|
# This is a custom FieldListFilter class for a given field.
|
2011-06-22 20:20:52 +08:00
|
|
|
field, field_list_filter_class = list_filter
|
2011-05-24 17:17:55 +08:00
|
|
|
else:
|
2011-05-03 18:44:23 +08:00
|
|
|
# This is simply a field name, so use the default
|
|
|
|
# FieldListFilter class that has been registered for
|
|
|
|
# the type of the given field.
|
2011-06-22 20:20:52 +08:00
|
|
|
field, field_list_filter_class = list_filter, FieldListFilter.create
|
2011-05-03 18:44:23 +08:00
|
|
|
if not isinstance(field, models.Field):
|
|
|
|
field_path = field
|
|
|
|
field = get_fields_from_path(self.model, field_path)[-1]
|
2016-03-29 06:33:29 +08:00
|
|
|
spec = field_list_filter_class(
|
|
|
|
field, request, lookup_params,
|
|
|
|
self.model, self.model_admin, field_path=field_path
|
|
|
|
)
|
2011-11-22 20:26:17 +08:00
|
|
|
# Check if we need to use distinct()
|
2016-03-29 06:33:29 +08:00
|
|
|
use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)
|
2006-05-02 09:31:56 +08:00
|
|
|
if spec and spec.has_output():
|
|
|
|
filter_specs.append(spec)
|
2011-11-22 20:26:17 +08:00
|
|
|
|
|
|
|
# At this point, all the parameters used by the various ListFilters
|
|
|
|
# have been removed from lookup_params, which now only contains other
|
|
|
|
# parameters passed via the query string. We now loop through the
|
|
|
|
# remaining parameters both to ensure that all the parameters are valid
|
2012-03-18 05:45:36 +08:00
|
|
|
# fields and to determine if at least one of them needs distinct(). If
|
|
|
|
# the lookup parameters aren't real fields, then bail out.
|
|
|
|
try:
|
|
|
|
for key, value in lookup_params.items():
|
|
|
|
lookup_params[key] = prepare_lookup_value(key, value)
|
2016-03-29 06:33:29 +08:00
|
|
|
use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, key)
|
2012-03-18 05:45:36 +08:00
|
|
|
return filter_specs, bool(filter_specs), lookup_params, use_distinct
|
2012-04-29 00:09:37 +08:00
|
|
|
except FieldDoesNotExist as e:
|
2013-03-19 13:04:59 +08:00
|
|
|
six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-06-03 21:37:34 +08:00
|
|
|
def get_query_string(self, new_params=None, remove=None):
|
2013-10-17 16:17:41 +08:00
|
|
|
if new_params is None:
|
|
|
|
new_params = {}
|
|
|
|
if remove is None:
|
|
|
|
remove = []
|
2006-05-02 09:31:56 +08:00
|
|
|
p = self.params.copy()
|
|
|
|
for r in remove:
|
2012-08-14 21:57:04 +08:00
|
|
|
for k in list(p):
|
2006-05-02 09:31:56 +08:00
|
|
|
if k.startswith(r):
|
|
|
|
del p[k]
|
|
|
|
for k, v in new_params.items():
|
2008-07-19 07:54:34 +08:00
|
|
|
if v is None:
|
|
|
|
if k in p:
|
|
|
|
del p[k]
|
|
|
|
else:
|
2006-05-02 09:31:56 +08:00
|
|
|
p[k] = v
|
2012-12-24 09:32:04 +08:00
|
|
|
return '?%s' % urlencode(sorted(p.items()))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get_results(self, request):
|
2013-03-08 22:15:23 +08:00
|
|
|
paginator = self.model_admin.get_paginator(request, self.queryset, self.list_per_page)
|
2006-05-02 09:31:56 +08:00
|
|
|
# Get the number of objects, with admin filters applied.
|
2008-10-23 07:09:35 +08:00
|
|
|
result_count = paginator.count
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Get the total number of objects, with no admin filters applied.
|
2014-09-23 00:46:43 +08:00
|
|
|
if self.model_admin.show_full_result_count:
|
2015-11-07 21:01:25 +08:00
|
|
|
full_result_count = self.root_queryset.count()
|
2013-02-24 20:18:21 +08:00
|
|
|
else:
|
2014-09-23 00:46:43 +08:00
|
|
|
full_result_count = None
|
2011-09-08 21:25:00 +08:00
|
|
|
can_show_all = result_count <= self.list_max_show_all
|
2008-07-19 07:54:34 +08:00
|
|
|
multi_page = result_count > self.list_per_page
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Get the list of objects to display on this page.
|
|
|
|
if (self.show_all and can_show_all) or not multi_page:
|
2013-03-08 22:15:23 +08:00
|
|
|
result_list = self.queryset._clone()
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
try:
|
2013-11-04 02:08:55 +08:00
|
|
|
result_list = paginator.page(self.page_num + 1).object_list
|
2006-05-02 09:31:56 +08:00
|
|
|
except InvalidPage:
|
2010-12-13 06:58:47 +08:00
|
|
|
raise IncorrectLookupParameters
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
self.result_count = result_count
|
2014-09-23 00:46:43 +08:00
|
|
|
self.show_full_result_count = self.model_admin.show_full_result_count
|
|
|
|
# Admin actions are shown if there is at least one entry
|
|
|
|
# or if entries are not counted because show_full_result_count is disabled
|
2015-03-05 01:00:12 +08:00
|
|
|
self.show_admin_actions = not self.show_full_result_count or bool(full_result_count)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.full_result_count = full_result_count
|
|
|
|
self.result_list = result_list
|
|
|
|
self.can_show_all = can_show_all
|
|
|
|
self.multi_page = multi_page
|
|
|
|
self.paginator = paginator
|
|
|
|
|
2011-06-03 19:54:29 +08:00
|
|
|
def _get_default_ordering(self):
|
|
|
|
ordering = []
|
|
|
|
if self.model_admin.ordering:
|
|
|
|
ordering = self.model_admin.ordering
|
|
|
|
elif self.lookup_opts.ordering:
|
|
|
|
ordering = self.lookup_opts.ordering
|
|
|
|
return ordering
|
|
|
|
|
2011-11-22 17:14:09 +08:00
|
|
|
def get_ordering_field(self, field_name):
|
|
|
|
"""
|
|
|
|
Returns the proper model field name corresponding to the given
|
|
|
|
field_name to use for ordering. field_name may either be the name of a
|
|
|
|
proper model field or the name of a method (on the admin or model) or a
|
|
|
|
callable with the 'admin_order_field' attribute. Returns None if no
|
|
|
|
proper model field name can be matched.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
field = self.lookup_opts.get_field(field_name)
|
|
|
|
return field.name
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2011-11-22 17:14:09 +08:00
|
|
|
# See whether field_name is a name of a non-field
|
|
|
|
# that allows sorting.
|
|
|
|
if callable(field_name):
|
|
|
|
attr = field_name
|
|
|
|
elif hasattr(self.model_admin, field_name):
|
|
|
|
attr = getattr(self.model_admin, field_name)
|
|
|
|
else:
|
|
|
|
attr = getattr(self.model, field_name)
|
|
|
|
return getattr(attr, 'admin_order_field', None)
|
|
|
|
|
2012-03-03 10:13:35 +08:00
|
|
|
def get_ordering(self, request, queryset):
|
|
|
|
"""
|
|
|
|
Returns the list of ordering fields for the change list.
|
|
|
|
First we check the get_ordering() method in model admin, then we check
|
|
|
|
the object's default ordering. Then, any manually-specified ordering
|
|
|
|
from the query string overrides anything. Finally, a deterministic
|
|
|
|
order is guaranteed by ensuring the primary key is used as the last
|
|
|
|
ordering field.
|
|
|
|
"""
|
2011-06-03 19:54:29 +08:00
|
|
|
params = self.params
|
2016-04-04 08:37:32 +08:00
|
|
|
ordering = list(self.model_admin.get_ordering(request) or self._get_default_ordering())
|
2007-04-26 21:30:48 +08:00
|
|
|
if ORDER_VAR in params:
|
2011-06-03 00:18:47 +08:00
|
|
|
# Clear ordering and used params
|
|
|
|
ordering = []
|
|
|
|
order_params = params[ORDER_VAR].split('.')
|
|
|
|
for p in order_params:
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2011-06-03 00:18:47 +08:00
|
|
|
none, pfx, idx = p.rpartition('-')
|
|
|
|
field_name = self.list_display[int(idx)]
|
2011-11-22 17:14:09 +08:00
|
|
|
order_field = self.get_ordering_field(field_name)
|
|
|
|
if not order_field:
|
2013-11-03 05:02:56 +08:00
|
|
|
continue # No 'admin_order_field', skip it
|
2014-01-23 10:13:59 +08:00
|
|
|
# reverse order if order_field has already "-" as prefix
|
|
|
|
if order_field.startswith('-') and pfx == "-":
|
|
|
|
ordering.append(order_field[1:])
|
|
|
|
else:
|
|
|
|
ordering.append(pfx + order_field)
|
2011-06-03 00:18:47 +08:00
|
|
|
except (IndexError, ValueError):
|
2013-11-03 05:02:56 +08:00
|
|
|
continue # Invalid ordering specified, skip it.
|
2012-03-03 10:13:35 +08:00
|
|
|
|
|
|
|
# Add the given query's ordering fields, if any.
|
|
|
|
ordering.extend(queryset.query.order_by)
|
|
|
|
|
|
|
|
# Ensure that the primary key is systematically present in the list of
|
|
|
|
# ordering fields so we can guarantee a deterministic order across all
|
|
|
|
# database backends.
|
|
|
|
pk_name = self.lookup_opts.pk.name
|
2014-09-26 20:31:50 +08:00
|
|
|
if not (set(ordering) & {'pk', '-pk', pk_name, '-' + pk_name}):
|
2012-03-03 10:13:35 +08:00
|
|
|
# The two sets do not intersect, meaning the pk isn't present. So
|
|
|
|
# we add it.
|
2012-04-09 12:28:32 +08:00
|
|
|
ordering.append('-pk')
|
2012-03-03 10:13:35 +08:00
|
|
|
|
2011-06-03 00:18:47 +08:00
|
|
|
return ordering
|
|
|
|
|
2011-06-03 19:54:29 +08:00
|
|
|
def get_ordering_field_columns(self):
|
2011-11-22 17:14:09 +08:00
|
|
|
"""
|
2013-08-03 13:41:15 +08:00
|
|
|
Returns an OrderedDict of ordering field column numbers and asc/desc
|
2011-11-22 17:14:09 +08:00
|
|
|
"""
|
2011-06-03 19:54:29 +08:00
|
|
|
|
|
|
|
# We must cope with more than one column having the same underlying sort
|
|
|
|
# field, so we base things on column numbers.
|
|
|
|
ordering = self._get_default_ordering()
|
2013-08-03 13:41:15 +08:00
|
|
|
ordering_fields = OrderedDict()
|
2011-06-03 19:54:29 +08:00
|
|
|
if ORDER_VAR not in self.params:
|
|
|
|
# for ordering specified on ModelAdmin or model Meta, we don't know
|
|
|
|
# the right column numbers absolutely, because there might be more
|
|
|
|
# than one column associated with that ordering, so we guess.
|
2011-11-18 05:30:07 +08:00
|
|
|
for field in ordering:
|
|
|
|
if field.startswith('-'):
|
|
|
|
field = field[1:]
|
2011-06-03 19:54:29 +08:00
|
|
|
order_type = 'desc'
|
|
|
|
else:
|
|
|
|
order_type = 'asc'
|
2011-11-22 17:14:09 +08:00
|
|
|
for index, attr in enumerate(self.list_display):
|
|
|
|
if self.get_ordering_field(attr) == field:
|
|
|
|
ordering_fields[index] = order_type
|
|
|
|
break
|
2011-06-03 19:54:29 +08:00
|
|
|
else:
|
|
|
|
for p in self.params[ORDER_VAR].split('.'):
|
|
|
|
none, pfx, idx = p.rpartition('-')
|
|
|
|
try:
|
|
|
|
idx = int(idx)
|
|
|
|
except ValueError:
|
2013-11-03 05:02:56 +08:00
|
|
|
continue # skip it
|
2011-06-03 19:54:29 +08:00
|
|
|
ordering_fields[idx] = 'desc' if pfx == '-' else 'asc'
|
2011-06-03 00:18:47 +08:00
|
|
|
return ordering_fields
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self, request):
|
2012-03-18 05:45:36 +08:00
|
|
|
# First, we collect all the declared list filters.
|
|
|
|
(self.filter_specs, self.has_filters, remaining_lookup_params,
|
2013-05-19 16:52:29 +08:00
|
|
|
filters_use_distinct) = self.get_filters(request)
|
2011-11-22 20:26:17 +08:00
|
|
|
|
2012-03-18 05:45:36 +08:00
|
|
|
# Then, we let every list filter modify the queryset to its liking.
|
2013-03-08 22:15:23 +08:00
|
|
|
qs = self.root_queryset
|
2012-03-18 05:45:36 +08:00
|
|
|
for filter_spec in self.filter_specs:
|
|
|
|
new_qs = filter_spec.queryset(request, qs)
|
|
|
|
if new_qs is not None:
|
|
|
|
qs = new_qs
|
2011-11-22 20:26:17 +08:00
|
|
|
|
2012-03-18 05:45:36 +08:00
|
|
|
try:
|
2011-11-22 20:26:17 +08:00
|
|
|
# Finally, we apply the remaining lookup parameters from the query
|
|
|
|
# string (i.e. those that haven't already been processed by the
|
|
|
|
# filters).
|
|
|
|
qs = qs.filter(**remaining_lookup_params)
|
|
|
|
except (SuspiciousOperation, ImproperlyConfigured):
|
|
|
|
# Allow certain types of errors to be re-raised as-is so that the
|
|
|
|
# caller can treat them in a special way.
|
|
|
|
raise
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2011-11-22 20:26:17 +08:00
|
|
|
# Every other error is caught with a naked except, because we don't
|
|
|
|
# have any other way of validating lookup parameters. They might be
|
|
|
|
# invalid if the keyword arguments are incorrect, or if the values
|
|
|
|
# are not in the correct type, so we might get FieldError,
|
2012-03-18 05:45:36 +08:00
|
|
|
# ValueError, ValidationError, or ?.
|
2011-05-03 18:44:23 +08:00
|
|
|
raise IncorrectLookupParameters(e)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-05-14 23:09:33 +08:00
|
|
|
if not qs.query.select_related:
|
2013-06-05 05:35:11 +08:00
|
|
|
qs = self.apply_select_related(qs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Set ordering.
|
2012-03-03 10:13:35 +08:00
|
|
|
ordering = self.get_ordering(request, qs)
|
|
|
|
qs = qs.order_by(*ordering)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-05-19 16:52:29 +08:00
|
|
|
# Apply search results
|
2016-03-29 06:33:29 +08:00
|
|
|
qs, search_use_distinct = self.model_admin.get_search_results(request, qs, self.query)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-06-05 05:35:11 +08:00
|
|
|
# Remove duplicates from results, if necessary
|
2013-05-19 16:52:29 +08:00
|
|
|
if filters_use_distinct | search_use_distinct:
|
2011-02-14 06:51:40 +08:00
|
|
|
return qs.distinct()
|
|
|
|
else:
|
|
|
|
return qs
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-06-05 05:35:11 +08:00
|
|
|
def apply_select_related(self, qs):
|
|
|
|
if self.list_select_related is True:
|
|
|
|
return qs.select_related()
|
|
|
|
|
|
|
|
if self.list_select_related is False:
|
|
|
|
if self.has_related_field_in_list_display():
|
|
|
|
return qs.select_related()
|
|
|
|
|
|
|
|
if self.list_select_related:
|
|
|
|
return qs.select_related(*self.list_select_related)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
def has_related_field_in_list_display(self):
|
|
|
|
for field_name in self.list_display:
|
|
|
|
try:
|
|
|
|
field = self.lookup_opts.get_field(field_name)
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2013-06-05 05:35:11 +08:00
|
|
|
pass
|
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(field.remote_field, models.ManyToOneRel):
|
2013-06-05 05:35:11 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def url_for_result(self, result):
|
2012-09-25 09:02:59 +08:00
|
|
|
pk = getattr(result, self.pk_attname)
|
|
|
|
return reverse('admin:%s_%s_change' % (self.opts.app_label,
|
2013-02-05 17:16:07 +08:00
|
|
|
self.opts.model_name),
|
2012-09-25 09:02:59 +08:00
|
|
|
args=(quote(pk),),
|
|
|
|
current_app=self.model_admin.admin_site.name)
|