Ticket #18086: 18086.admin-changelist-order-pk-desc.diff

File 18086.admin-changelist-order-pk-desc.diff, 7.7 KB (added by Julien Phalip, 13 years ago)
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index b11f9d5..9d5c304 100644
    a b class ChangeList(object):  
    258258        if not (set(ordering) & set(['pk', '-pk', pk_name, '-' + pk_name])):
    259259            # The two sets do not intersect, meaning the pk isn't present. So
    260260            # we add it.
    261             ordering.append('pk')
     261            ordering.append('-pk')
    262262
    263263        return ordering
    264264
  • tests/regressiontests/admin_changelist/models.py

    diff --git a/tests/regressiontests/admin_changelist/models.py b/tests/regressiontests/admin_changelist/models.py
    index 6c0b789..1c615ea 100644
    a b class UnorderedObject(models.Model):  
    6969
    7070class OrderedObjectManager(models.Manager):
    7171    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')
    7373
    7474class OrderedObject(models.Model):
    7575    """
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index 7e4546e..f4cdbde 100644
    a b class ChangeListTests(TestCase):  
    446446        class UnorderedObjectAdmin(admin.ModelAdmin):
    447447            list_per_page = 10
    448448
    449         def check_results_order(reverse=False):
     449        def check_results_order(ascending=False):
    450450            admin.site.register(UnorderedObject, UnorderedObjectAdmin)
    451451            model_admin = UnorderedObjectAdmin(UnorderedObject, admin.site)
    452             counter = 51 if reverse else 0
     452            counter = 0 if ascending else 51
    453453            for page in range (0, 5):
    454454                request = self._mocked_authenticated_request('/unorderedobject/?p=%s' % page, superuser)
    455455                response = model_admin.changelist_view(request)
    456456                for result in response.context_data['cl'].result_list:
    457                     counter += -1 if reverse else 1
     457                    counter += 1 if ascending else -1
    458458                    self.assertEqual(result.id, counter)
    459459            admin.site.unregister(UnorderedObject)
    460460
    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'.
    462462        check_results_order()
    463463
    464464        # 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.
    466466        UnorderedObjectAdmin.ordering = ['bool']
    467467        check_results_order()
    468468
    469469        # When order fields are defined, including the pk itself, use them.
    470470        UnorderedObjectAdmin.ordering = ['bool', '-pk']
    471         check_results_order(reverse=True)
    472         UnorderedObjectAdmin.ordering = ['bool', 'pk']
    473471        check_results_order()
     472        UnorderedObjectAdmin.ordering = ['bool', 'pk']
     473        check_results_order(ascending=True)
    474474        UnorderedObjectAdmin.ordering = ['-id', 'bool']
    475         check_results_order(reverse=True)
    476         UnorderedObjectAdmin.ordering = ['id', 'bool']
    477475        check_results_order()
     476        UnorderedObjectAdmin.ordering = ['id', 'bool']
     477        check_results_order(ascending=True)
    478478
    479479    def test_deterministic_order_for_model_ordered_by_its_manager(self):
    480480        """
    class ChangeListTests(TestCase):  
    491491        class OrderedObjectAdmin(admin.ModelAdmin):
    492492            list_per_page = 10
    493493
    494         def check_results_order(reverse=False):
     494        def check_results_order(ascending=False):
    495495            admin.site.register(OrderedObject, OrderedObjectAdmin)
    496496            model_admin = OrderedObjectAdmin(OrderedObject, admin.site)
    497             counter = 51 if reverse else 0
     497            counter = 0 if ascending else 51
    498498            for page in range (0, 5):
    499499                request = self._mocked_authenticated_request('/orderedobject/?p=%s' % page, superuser)
    500500                response = model_admin.changelist_view(request)
    501501                for result in response.context_data['cl'].result_list:
    502                     counter += -1 if reverse else 1
     502                    counter += 1 if ascending else -1
    503503                    self.assertEqual(result.id, counter)
    504504            admin.site.unregister(OrderedObject)
    505505
    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)
    508508
    509509        # 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.
    511511        OrderedObjectAdmin.ordering = ['bool']
    512512        check_results_order()
    513513
    514514        # When order fields are defined, including the pk itself, use them.
    515515        OrderedObjectAdmin.ordering = ['bool', '-pk']
    516         check_results_order(reverse=True)
    517         OrderedObjectAdmin.ordering = ['bool', 'pk']
    518516        check_results_order()
     517        OrderedObjectAdmin.ordering = ['bool', 'pk']
     518        check_results_order(ascending=True)
    519519        OrderedObjectAdmin.ordering = ['-id', 'bool']
    520         check_results_order(reverse=True)
     520        check_results_order()
    521521        OrderedObjectAdmin.ordering = ['id', 'bool']
    522         check_results_order()
    523  No newline at end of file
     522        check_results_order(ascending=True)
     523 No newline at end of file
  • tests/regressiontests/admin_filters/tests.py

    diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py
    index d87c447..e2a12c9 100644
    a b class ListFiltersTests(TestCase):  
    654654
    655655        # Make sure the correct queryset is returned
    656656        queryset = changelist.get_query_set(request)
    657         self.assertEqual(list(queryset), [john, jack])
     657        self.assertEqual(list(queryset), [jack, john])
    658658
    659659        filterspec = changelist.get_filters(request)[0][-1]
    660660        self.assertEqual(force_unicode(filterspec.title), u'department')
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 564e492..2a389fd 100644
    a b class AdminViewListEditable(TestCase):  
    18941894        UnorderedObject.objects.create(id=2, name='Unordered object #2')
    18951895        UnorderedObject.objects.create(id=3, name='Unordered object #3')
    18961896        response = self.client.get('/test_admin/admin/admin_views/unorderedobject/')
    1897         self.assertContains(response, 'Unordered object #1')
     1897        self.assertContains(response, 'Unordered object #3')
    18981898        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')
    19011899        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')
    19021902        self.assertNotContains(response, 'Unordered object #2')
    1903         self.assertContains(response, 'Unordered object #3')
     1903        self.assertContains(response, 'Unordered object #1')
    19041904
    19051905    def test_list_editable_action_submit(self):
    19061906        # List editable changes should not be executed if the action "Go" button is
Back to Top