[Django] #37233: Admin renders a sort control for every "__str__" column

4 views
Skip to first unread message

Django

unread,
Jul 26, 2026, 1:23:20 PM (3 days ago) Jul 26
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
------------------------+-----------------------------------------
Reporter: blighj | Type: Bug
Status: new | Component: contrib.admin
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+-----------------------------------------
Since ticket:10743#comment:26, any `__str__` column in an admin list view
is rendered as a sortable column, regardless of whether it has been linked
to a column and marked as sortable.
This simple model would reproduce it.
{{{#!python
# models.py
class SortingTest(models.Model):
name = models.CharField(max_length=30)

def __str__(self):
return self.name


# admin.py
admin.site.register(SortingTest)
}}}
list_display defaults to `("__str__",)` but no sorting has been
configured for the model's `__str__`, so the column should not be
sortable.

`result_headers()` in `django/contrib/admin/templatetags/admin_list.py`
decides sortability with a substring test:

{{{#!python
if not admin_order_field and LOOKUP_SEP not in field_name:
is_field_sortable = False
}}}

`LOOKUP_SEP` is `"__"` an so `__str__` flags as sortable

`ChangeList.get_ordering_field()` resolves the same name by attribute
lookup
rather than by checking for double underscores, correctly finds no
`admin_order_field`, and
`get_ordering()` drops it:

{{{#!python
order_field = self.get_ordering_field(field_name)
if not order_field:
continue
}}}

Potentially result_headers could use get_ordering_field or some other
technique.

This showed up reviewing ticket:27752#comment:16, which has the bigger
issue that sorting on `__str__` doesn't work anyway. I don't judge this as
a huge issue as once you need sorting you are most likely using specific
list_dispaly values.
--
Ticket URL: <https://code.djangoproject.com/ticket/37233>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 27, 2026, 4:28:12 AM (2 days ago) Jul 27
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+------------------------------------
Reporter: blighj | Owner: (none)
Type: Bug | Status: new
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Well spotted! Thank you
--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:1>

Django

unread,
Jul 27, 2026, 7:52:56 AM (2 days ago) Jul 27
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+-----------------------------------------
Reporter: blighj | Owner: Akshat Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Akshat Sparsh):

* owner: (none) => Akshat Sparsh
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:2>

Django

unread,
Jul 27, 2026, 7:55:25 AM (2 days ago) Jul 27
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+-----------------------------------------
Reporter: blighj | Owner: Akshat Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Akshat Sparsh):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/21678 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:3>

Django

unread,
Jul 27, 2026, 8:02:02 AM (2 days ago) Jul 27
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+-----------------------------------------
Reporter: blighj | Owner: Akshat Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Comment (by Akshat Sparsh):

Replacement PR: [https://github.com/django/django/pull/21679 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:4>

Django

unread,
Jul 27, 2026, 6:47:46 PM (2 days ago) Jul 27
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+-----------------------------------------
Reporter: blighj | Owner: Akshat Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by blighj):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:5>

Django

unread,
Jul 28, 2026, 3:02:56 AM (yesterday) Jul 28
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------+-----------------------------------------
Reporter: blighj | Owner: Akshat Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Akshat Sparsh):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:6>

Django

unread,
Jul 28, 2026, 6:52:30 AM (yesterday) Jul 28
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------------+-------------------------------------
Reporter: blighj | Owner: Akshat
| Sparsh
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blighj):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:7>

Django

unread,
8:04 AM (7 hours ago) 8:04 AM
to django-...@googlegroups.com
#37233: Admin renders a sort control for every "__str__" column
-------------------------------------+-------------------------------------
Reporter: blighj | Owner: Akshat
| Sparsh
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"92e1d9e3619ae5274a64b38f26177064486892f2" 92e1d9e]:
{{{#!CommitTicketReference repository=""
revision="92e1d9e3619ae5274a64b38f26177064486892f2"
Fixed #37233 -- Prevented sort controls for unordered __str__ admin
columns.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/37233#comment:8>
Reply all
Reply to author
Forward
0 new messages