Fixed #18086 -- Restored '-pk' as the default order in the admin changelist. This rectifies a slight change in behavior introduced in Django 1.4 and r17635.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17881 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2012-04-09 04:28:32 +00:00
parent 93d1fdb130
commit a0b55f30a8
5 changed files with 28 additions and 28 deletions

View File

@ -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

View File

@ -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):
"""

View File

@ -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()
check_results_order(ascending=True)

View File

@ -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')

View File

@ -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