#35747: Admin change list doesn't redirect to default ordering when the last
ordering field is removed from sorting
-------------------------------------+-------------------------------------
     Reporter:  ldeluigi             |                    Owner:  ldeluigi
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  5.1
     Severity:  Normal               |               Resolution:
     Keywords:  admin change list    |             Triage Stage:  Accepted
  ordering sorting sortremove        |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  1
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

 * easy:  1 => 0
 * needs_tests:  0 => 1
 * owner:  (none) => ldeluigi
 * stage:  Unreviewed => Accepted
 * status:  new => assigned

Comment:

 Thank you for the clarification

 Have a test and an alternative patch. Needs testing in the admin but I
 think I've replicated the behavior here
 {{{#!diff
 --- a/django/contrib/admin/views/main.py
 +++ b/django/contrib/admin/views/main.py
 @@ -395,7 +395,7 @@ class ChangeList:
          ordering = list(
              self.model_admin.get_ordering(request) or
 self._get_default_ordering()
          )
 -        if ORDER_VAR in params:
 +        if ORDER_VAR in params and params[ORDER_VAR]:
              # Clear ordering and used params
              ordering = []
              order_params = params[ORDER_VAR].split(".")
 diff --git a/tests/admin_changelist/tests.py
 b/tests/admin_changelist/tests.py
 index 694f807781..5324f39364 100644
 --- a/tests/admin_changelist/tests.py
 +++ b/tests/admin_changelist/tests.py
 @@ -1375,6 +1375,20 @@ class ChangeListTests(TestCase):
          OrderedObjectAdmin.ordering = ["id", "bool"]
          check_results_order(ascending=True)

 +    def test_ordering_from_model_meta(self):
 +        Swallow.objects.create(origin="Swallow A", load=4, speed=2)
 +        Swallow.objects.create(origin="Swallow B", load=2, speed=1)
 +        Swallow.objects.create(origin="Swallow C", load=5, speed=1)
 +        m = SwallowAdmin(Swallow, custom_site)
 +        request = self._mocked_authenticated_request("/swallow/?o=",
 self.superuser)
 +        changelist = m.get_changelist_instance(request)
 +        queryset = changelist.get_queryset(request)
 +        self.assertQuerySetEqual(
 +            queryset,
 +            [(1.0, 2.0), (1.0, 5.0), (2.0, 4.0)],
 +            lambda s: (s.speed, s.load),
 +        )
 +
      @isolate_apps("admin_changelist")
      def test_total_ordering_optimization(self):
          class Related(models.Model):
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35747#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070191dad69782-ff3d76b7-52dc-46f8-99b3-e848aad7ef48-000000%40eu-central-1.amazonses.com.

Reply via email to