Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18086 -- Restored '-pk' as the default order in the admin chan…

…gelist. 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
  • Loading branch information...
commit a0b55f30a843a9ce12a907d65edcd479f5a5b1cb 1 parent 93d1fdb
Julien Phalip authored April 09, 2012
2  django/contrib/admin/views/main.py
@@ -258,7 +258,7 @@ def get_ordering(self, request, queryset):
258 258
         if not (set(ordering) & set(['pk', '-pk', pk_name, '-' + pk_name])):
259 259
             # The two sets do not intersect, meaning the pk isn't present. So
260 260
             # we add it.
261  
-            ordering.append('pk')
  261
+            ordering.append('-pk')
262 262
 
263 263
         return ordering
264 264
 
2  tests/regressiontests/admin_changelist/models.py
@@ -69,7 +69,7 @@ class UnorderedObject(models.Model):
69 69
 
70 70
 class OrderedObjectManager(models.Manager):
71 71
     def get_query_set(self):
72  
-        return super(OrderedObjectManager, self).get_query_set().order_by('-number')
  72
+        return super(OrderedObjectManager, self).get_query_set().order_by('number')
73 73
 
74 74
 class OrderedObject(models.Model):
75 75
     """
38  tests/regressiontests/admin_changelist/tests.py
@@ -446,35 +446,35 @@ def test_deterministic_order_for_unordered_model(self):
446 446
         class UnorderedObjectAdmin(admin.ModelAdmin):
447 447
             list_per_page = 10
448 448
 
449  
-        def check_results_order(reverse=False):
  449
+        def check_results_order(ascending=False):
450 450
             admin.site.register(UnorderedObject, UnorderedObjectAdmin)
451 451
             model_admin = UnorderedObjectAdmin(UnorderedObject, admin.site)
452  
-            counter = 51 if reverse else 0
  452
+            counter = 0 if ascending else 51
453 453
             for page in range (0, 5):
454 454
                 request = self._mocked_authenticated_request('/unorderedobject/?p=%s' % page, superuser)
455 455
                 response = model_admin.changelist_view(request)
456 456
                 for result in response.context_data['cl'].result_list:
457  
-                    counter += -1 if reverse else 1
  457
+                    counter += 1 if ascending else -1
458 458
                     self.assertEqual(result.id, counter)
459 459
             admin.site.unregister(UnorderedObject)
460 460
 
461  
-        # When no order is defined at all, everything is ordered by 'pk'.
  461
+        # When no order is defined at all, everything is ordered by '-pk'.
462 462
         check_results_order()
463 463
 
464 464
         # When an order field is defined but multiple records have the same
465  
-        # value for that field, make sure everything gets ordered by pk as well.
  465
+        # value for that field, make sure everything gets ordered by -pk as well.
466 466
         UnorderedObjectAdmin.ordering = ['bool']
467 467
         check_results_order()
468 468
 
469 469
         # When order fields are defined, including the pk itself, use them.
470 470
         UnorderedObjectAdmin.ordering = ['bool', '-pk']
471  
-        check_results_order(reverse=True)
472  
-        UnorderedObjectAdmin.ordering = ['bool', 'pk']
473 471
         check_results_order()
  472
+        UnorderedObjectAdmin.ordering = ['bool', 'pk']
  473
+        check_results_order(ascending=True)
474 474
         UnorderedObjectAdmin.ordering = ['-id', 'bool']
475  
-        check_results_order(reverse=True)
476  
-        UnorderedObjectAdmin.ordering = ['id', 'bool']
477 475
         check_results_order()
  476
+        UnorderedObjectAdmin.ordering = ['id', 'bool']
  477
+        check_results_order(ascending=True)
478 478
 
479 479
     def test_deterministic_order_for_model_ordered_by_its_manager(self):
480 480
         """
@@ -491,32 +491,32 @@ def test_deterministic_order_for_model_ordered_by_its_manager(self):
491 491
         class OrderedObjectAdmin(admin.ModelAdmin):
492 492
             list_per_page = 10
493 493
 
494  
-        def check_results_order(reverse=False):
  494
+        def check_results_order(ascending=False):
495 495
             admin.site.register(OrderedObject, OrderedObjectAdmin)
496 496
             model_admin = OrderedObjectAdmin(OrderedObject, admin.site)
497  
-            counter = 51 if reverse else 0
  497
+            counter = 0 if ascending else 51
498 498
             for page in range (0, 5):
499 499
                 request = self._mocked_authenticated_request('/orderedobject/?p=%s' % page, superuser)
500 500
                 response = model_admin.changelist_view(request)
501 501
                 for result in response.context_data['cl'].result_list:
502  
-                    counter += -1 if reverse else 1
  502
+                    counter += 1 if ascending else -1
503 503
                     self.assertEqual(result.id, counter)
504 504
             admin.site.unregister(OrderedObject)
505 505
 
506  
-        # When no order is defined at all, use the model's default ordering (i.e. '-number')
507  
-        check_results_order(reverse=True)
  506
+        # When no order is defined at all, use the model's default ordering (i.e. 'number')
  507
+        check_results_order(ascending=True)
508 508
 
509 509
         # When an order field is defined but multiple records have the same
510  
-        # value for that field, make sure everything gets ordered by pk as well.
  510
+        # value for that field, make sure everything gets ordered by -pk as well.
511 511
         OrderedObjectAdmin.ordering = ['bool']
512 512
         check_results_order()
513 513
 
514 514
         # When order fields are defined, including the pk itself, use them.
515 515
         OrderedObjectAdmin.ordering = ['bool', '-pk']
516  
-        check_results_order(reverse=True)
517  
-        OrderedObjectAdmin.ordering = ['bool', 'pk']
518 516
         check_results_order()
  517
+        OrderedObjectAdmin.ordering = ['bool', 'pk']
  518
+        check_results_order(ascending=True)
519 519
         OrderedObjectAdmin.ordering = ['-id', 'bool']
520  
-        check_results_order(reverse=True)
  520
+        check_results_order()
521 521
         OrderedObjectAdmin.ordering = ['id', 'bool']
522  
-        check_results_order()
  522
+        check_results_order(ascending=True)
2  tests/regressiontests/admin_filters/tests.py
@@ -654,7 +654,7 @@ def test_fk_with_to_field(self):
654 654
 
655 655
         # Make sure the correct queryset is returned
656 656
         queryset = changelist.get_query_set(request)
657  
-        self.assertEqual(list(queryset), [john, jack])
  657
+        self.assertEqual(list(queryset), [jack, john])
658 658
 
659 659
         filterspec = changelist.get_filters(request)[0][-1]
660 660
         self.assertEqual(force_unicode(filterspec.title), u'department')
8  tests/regressiontests/admin_views/tests.py
@@ -1894,13 +1894,13 @@ def test_list_editable_pagination(self):
1894 1894
         UnorderedObject.objects.create(id=2, name='Unordered object #2')
1895 1895
         UnorderedObject.objects.create(id=3, name='Unordered object #3')
1896 1896
         response = self.client.get('/test_admin/admin/admin_views/unorderedobject/')
1897  
-        self.assertContains(response, 'Unordered object #1')
  1897
+        self.assertContains(response, 'Unordered object #3')
1898 1898
         self.assertContains(response, 'Unordered object #2')
1899  
-        self.assertNotContains(response, 'Unordered object #3')
1900  
-        response = self.client.get('/test_admin/admin/admin_views/unorderedobject/?p=1')
1901 1899
         self.assertNotContains(response, 'Unordered object #1')
  1900
+        response = self.client.get('/test_admin/admin/admin_views/unorderedobject/?p=1')
  1901
+        self.assertNotContains(response, 'Unordered object #3')
1902 1902
         self.assertNotContains(response, 'Unordered object #2')
1903  
-        self.assertContains(response, 'Unordered object #3')
  1903
+        self.assertContains(response, 'Unordered object #1')
1904 1904
 
1905 1905
     def test_list_editable_action_submit(self):
1906 1906
         # List editable changes should not be executed if the action "Go" button is

0 notes on commit a0b55f3

Please sign in to comment.
Something went wrong with that request. Please try again.