Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ModelAdmin.get_search_fields.

  • Loading branch information...
commit 0bcdcc7eb9ca2265cb83225257df65a8def3e14a 1 parent a0c5811
@loic loic authored timgraham committed
View
19 django/contrib/admin/options.py
@@ -277,9 +277,10 @@ def construct_search(field_name):
return "%s__icontains" % field_name
use_distinct = False
- if self.search_fields and search_term:
+ search_fields = self.get_search_fields(request)
+ if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
- for search_field in self.search_fields]
+ for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
@@ -767,6 +768,13 @@ def get_list_filter(self, request):
"""
return self.list_filter
+ def get_search_fields(self, request):
+ """
+ Returns a sequence containing the fields to be searched whenever
+ somebody submits a search query.
+ """
+ return self.search_fields
+
def get_preserved_filters(self, request):
"""
Returns the preserved filters querystring.
@@ -1243,6 +1251,7 @@ def changelist_view(self, request, extra_context=None):
list_display = self.get_list_display(request)
list_display_links = self.get_list_display_links(request, list_display)
list_filter = self.get_list_filter(request)
+ search_fields = self.get_search_fields(request)
# Check actions to see if any are available on this changelist
actions = self.get_actions(request)
@@ -1254,9 +1263,9 @@ def changelist_view(self, request, extra_context=None):
try:
cl = ChangeList(request, self.model, list_display,
list_display_links, list_filter, self.date_hierarchy,
- self.search_fields, self.list_select_related,
- self.list_per_page, self.list_max_show_all, self.list_editable,
- self)
+ search_fields, self.list_select_related, self.list_per_page,
+ self.list_max_show_all, self.list_editable, self)
+
except IncorrectLookupParameters:
# Wacky lookup parameters were given, so redirect to the main
# changelist page, without parameters, and pass an 'invalid=1'
View
8 docs/ref/contrib/admin/index.txt
@@ -1233,6 +1233,14 @@ templates used by the :class:`ModelAdmin` views:
to return the same kind of sequence type as for the
:attr:`~ModelAdmin.list_filter` attribute.
+.. method:: ModelAdmin.get_search_fields(self, request)
+
+ .. versionadded:: 1.7
+
+ The ``get_search_fields`` method is given the ``HttpRequest`` and is expected
+ to return the same kind of sequence type as for the
+ :attr:`~ModelAdmin.search_fields` attribute.
+
.. method:: ModelAdmin.get_inline_instances(self, request, obj=None)
.. versionadded:: 1.5
View
3  docs/releases/1.7.txt
@@ -110,6 +110,9 @@ Minor features
or ``None`` for the key and the custom label as the value. The default blank
option ``"----------"`` will be omitted in this case.
+* The admin's search fields can now be customized per-request thanks to the new
+ :meth:`django.contrib.admin.ModelAdmin.get_search_fields` method.
+
Backwards incompatible changes in 1.7
=====================================
View
7 tests/admin_changelist/admin.py
@@ -105,3 +105,10 @@ def get_list_filter(self, request):
my_list_filter.remove('parent')
return my_list_filter
+class DynamicSearchFieldsChildAdmin(admin.ModelAdmin):
+ search_fields = ('name',)
+
+ def get_search_fields(self, request):
+ search_fields = super(DynamicSearchFieldsChildAdmin, self).get_search_fields(request)
+ search_fields += ('age',)
+ return search_fields
View
10 tests/admin_changelist/tests.py
@@ -18,7 +18,8 @@
GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin,
DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin,
FilteredChildAdmin, CustomPaginator, site as custom_site,
- SwallowAdmin, DynamicListFilterChildAdmin, InvitationAdmin)
+ SwallowAdmin, DynamicListFilterChildAdmin, InvitationAdmin,
+ DynamicSearchFieldsChildAdmin)
from .models import (Event, Child, Parent, Genre, Band, Musician, Group,
Quartet, Membership, ChordsMusician, ChordsBand, Invitation, Swallow,
UnorderedObject, OrderedObject, CustomIdUser)
@@ -588,6 +589,13 @@ def test_dynamic_list_filter(self):
response = m.changelist_view(request)
self.assertEqual(response.context_data['cl'].list_filter, ('parent', 'name', 'age'))
+ def test_dynamic_search_fields(self):
+ child = self._create_superuser('child')
+ m = DynamicSearchFieldsChildAdmin(Child, admin.site)
+ request = self._mocked_authenticated_request('/child/', child)
+ response = m.changelist_view(request)
+ self.assertEqual(response.context_data['cl'].search_fields, ('name', 'age'))
+
def test_pagination_page_range(self):
"""
Regression tests for ticket #15653: ensure the number of pages
Please sign in to comment.
Something went wrong with that request. Please try again.