diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index d0f0350407..635330f6a8 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -52,8 +52,6 @@ class ChangeList(object): self.params = dict(request.GET.items()) if PAGE_VAR in self.params: del self.params[PAGE_VAR] - if TO_FIELD_VAR in self.params: - del self.params[TO_FIELD_VAR] if ERROR_FLAG in self.params: del self.params[ERROR_FLAG] @@ -173,7 +171,7 @@ class ChangeList(object): qs = self.root_query_set lookup_params = self.params.copy() # a dictionary of the query string - for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR): + for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR): if i in lookup_params: del lookup_params[i] for key, value in lookup_params.items(): diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 7782f9eab7..6be07b462d 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -1694,6 +1694,16 @@ class AdminSearchTest(TestCase): # confirm the search returned 1 object self.assertContains(response, "\n1 recommendation\n") + def test_with_fk_to_field(self): + """Ensure that the to_field GET parameter is preserved when a search + is performed. Refs #10918. + """ + from django.contrib.admin.views.main import TO_FIELD_VAR + response = self.client.get('/test_admin/admin/auth/user/?q=joe&%s=username' % TO_FIELD_VAR) + self.assertContains(response, "\n1 user\n") + self.assertContains(response, '') + + class AdminInheritedInlinesTest(TestCase): fixtures = ['admin-views-users.xml',]