diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index b11f9d566b9..9d5c30434d7 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -258,7 +258,7 @@ class ChangeList(object): if not (set(ordering) & set(['pk', '-pk', pk_name, '-' + pk_name])): # The two sets do not intersect, meaning the pk isn't present. So # we add it. - ordering.append('pk') + ordering.append('-pk') return ordering diff --git a/tests/regressiontests/admin_changelist/models.py b/tests/regressiontests/admin_changelist/models.py index 6c0b78982af..1c615eab194 100644 --- a/tests/regressiontests/admin_changelist/models.py +++ b/tests/regressiontests/admin_changelist/models.py @@ -69,7 +69,7 @@ class UnorderedObject(models.Model): class OrderedObjectManager(models.Manager): def get_query_set(self): - return super(OrderedObjectManager, self).get_query_set().order_by('-number') + return super(OrderedObjectManager, self).get_query_set().order_by('number') class OrderedObject(models.Model): """ diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py index 7e4546e5b36..f4cdbde4fb5 100644 --- a/tests/regressiontests/admin_changelist/tests.py +++ b/tests/regressiontests/admin_changelist/tests.py @@ -446,35 +446,35 @@ class ChangeListTests(TestCase): class UnorderedObjectAdmin(admin.ModelAdmin): list_per_page = 10 - def check_results_order(reverse=False): + def check_results_order(ascending=False): admin.site.register(UnorderedObject, UnorderedObjectAdmin) model_admin = UnorderedObjectAdmin(UnorderedObject, admin.site) - counter = 51 if reverse else 0 + counter = 0 if ascending else 51 for page in range (0, 5): request = self._mocked_authenticated_request('/unorderedobject/?p=%s' % page, superuser) response = model_admin.changelist_view(request) for result in response.context_data['cl'].result_list: - counter += -1 if reverse else 1 + counter += 1 if ascending else -1 self.assertEqual(result.id, counter) admin.site.unregister(UnorderedObject) - # When no order is defined at all, everything is ordered by 'pk'. + # When no order is defined at all, everything is ordered by '-pk'. check_results_order() # When an order field is defined but multiple records have the same - # value for that field, make sure everything gets ordered by pk as well. + # value for that field, make sure everything gets ordered by -pk as well. UnorderedObjectAdmin.ordering = ['bool'] check_results_order() # When order fields are defined, including the pk itself, use them. UnorderedObjectAdmin.ordering = ['bool', '-pk'] - check_results_order(reverse=True) + check_results_order() UnorderedObjectAdmin.ordering = ['bool', 'pk'] - check_results_order() + check_results_order(ascending=True) UnorderedObjectAdmin.ordering = ['-id', 'bool'] - check_results_order(reverse=True) - UnorderedObjectAdmin.ordering = ['id', 'bool'] check_results_order() + UnorderedObjectAdmin.ordering = ['id', 'bool'] + check_results_order(ascending=True) def test_deterministic_order_for_model_ordered_by_its_manager(self): """ @@ -491,32 +491,32 @@ class ChangeListTests(TestCase): class OrderedObjectAdmin(admin.ModelAdmin): list_per_page = 10 - def check_results_order(reverse=False): + def check_results_order(ascending=False): admin.site.register(OrderedObject, OrderedObjectAdmin) model_admin = OrderedObjectAdmin(OrderedObject, admin.site) - counter = 51 if reverse else 0 + counter = 0 if ascending else 51 for page in range (0, 5): request = self._mocked_authenticated_request('/orderedobject/?p=%s' % page, superuser) response = model_admin.changelist_view(request) for result in response.context_data['cl'].result_list: - counter += -1 if reverse else 1 + counter += 1 if ascending else -1 self.assertEqual(result.id, counter) admin.site.unregister(OrderedObject) - # When no order is defined at all, use the model's default ordering (i.e. '-number') - check_results_order(reverse=True) + # When no order is defined at all, use the model's default ordering (i.e. 'number') + check_results_order(ascending=True) # When an order field is defined but multiple records have the same - # value for that field, make sure everything gets ordered by pk as well. + # value for that field, make sure everything gets ordered by -pk as well. OrderedObjectAdmin.ordering = ['bool'] check_results_order() # When order fields are defined, including the pk itself, use them. OrderedObjectAdmin.ordering = ['bool', '-pk'] - check_results_order(reverse=True) - OrderedObjectAdmin.ordering = ['bool', 'pk'] check_results_order() + OrderedObjectAdmin.ordering = ['bool', 'pk'] + check_results_order(ascending=True) OrderedObjectAdmin.ordering = ['-id', 'bool'] - check_results_order(reverse=True) + check_results_order() OrderedObjectAdmin.ordering = ['id', 'bool'] - check_results_order() \ No newline at end of file + check_results_order(ascending=True) \ No newline at end of file diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index d87c447e302..e2a12c96633 100644 --- a/tests/regressiontests/admin_filters/tests.py +++ b/tests/regressiontests/admin_filters/tests.py @@ -654,7 +654,7 @@ class ListFiltersTests(TestCase): # Make sure the correct queryset is returned queryset = changelist.get_query_set(request) - self.assertEqual(list(queryset), [john, jack]) + self.assertEqual(list(queryset), [jack, john]) filterspec = changelist.get_filters(request)[0][-1] self.assertEqual(force_unicode(filterspec.title), u'department') diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 564e4924bbb..2a389fd5ec7 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -1894,13 +1894,13 @@ class AdminViewListEditable(TestCase): UnorderedObject.objects.create(id=2, name='Unordered object #2') UnorderedObject.objects.create(id=3, name='Unordered object #3') response = self.client.get('/test_admin/admin/admin_views/unorderedobject/') - self.assertContains(response, 'Unordered object #1') - self.assertContains(response, 'Unordered object #2') - self.assertNotContains(response, 'Unordered object #3') - response = self.client.get('/test_admin/admin/admin_views/unorderedobject/?p=1') - self.assertNotContains(response, 'Unordered object #1') - self.assertNotContains(response, 'Unordered object #2') self.assertContains(response, 'Unordered object #3') + self.assertContains(response, 'Unordered object #2') + self.assertNotContains(response, 'Unordered object #1') + response = self.client.get('/test_admin/admin/admin_views/unorderedobject/?p=1') + self.assertNotContains(response, 'Unordered object #3') + self.assertNotContains(response, 'Unordered object #2') + self.assertContains(response, 'Unordered object #1') def test_list_editable_action_submit(self): # List editable changes should not be executed if the action "Go" button is