[Django] #31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns

21 views
Skip to first unread message

Django

unread,
Sep 2, 2020, 4:54:38 AM9/2/20
to django-...@googlegroups.com
#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns
-----------------------------------------+------------------------
Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.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 |
-----------------------------------------+------------------------
If I try to assign list to `admin_order_field` to sort on multiple fields
(like `Model.Meta.ordering` does) like this:


{{{
@admin.register(Invoice)
class InvoiceAdmin(ModelAdmin):

def get_full_number(self, invoice):
return invoice.full_number
get_full_number.admin_order_field = ['issued__year', 'issued__month',
'-number']
}}}

I got following error:
{{{
File "/home/petr/.local/share/virtualenvs/blenderhub_server-
eFlwzMqz/lib/python3.7/site-packages/django/contrib/admin/options.py",
line 1693, in changelist_view
cl = self.get_changelist_instance(request)
File "/home/petr/.local/share/virtualenvs/blenderhub_server-
eFlwzMqz/lib/python3.7/site-packages/django/contrib/admin/options.py",
line 748, in get_changelist_instance
sortable_by,
File "/home/petr/.local/share/virtualenvs/blenderhub_server-
eFlwzMqz/lib/python3.7/site-packages/django/contrib/admin/views/main.py",
line 99, in __init__
self.queryset = self.get_queryset(request)
File "/home/petr/.local/share/virtualenvs/blenderhub_server-
eFlwzMqz/lib/python3.7/site-packages/django/contrib/admin/views/main.py",
line 479, in get_queryset
ordering = self.get_ordering(request, qs)
File "/home/petr/.local/share/virtualenvs/blenderhub_server-
eFlwzMqz/lib/python3.7/site-packages/django/contrib/admin/views/main.py",
line 328, in get_ordering
elif order_field.startswith('-') and pfx == '-':
AttributeError: 'list' object has no attribute 'startswith'
}}}

This is similar to https://code.djangoproject.com/ticket/30981 which has
been fixed, but sorting on multiple fields is still hard to achieve.

--
Ticket URL: <https://code.djangoproject.com/ticket/31975>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 2, 2020, 4:54:57 AM9/2/20
to django-...@googlegroups.com
#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns
-------------------------------------+-------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution:
Keywords: admin_order_field, | Triage Stage:
ModelAdmin | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petr Dlouhý):

* keywords: => admin_order_field, ModelAdmin


--
Ticket URL: <https://code.djangoproject.com/ticket/31975#comment:1>

Django

unread,
Sep 2, 2020, 5:16:20 AM9/2/20
to django-...@googlegroups.com
#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns
-------------------------------------+-------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution: invalid
Keywords: admin_order_field, | Triage Stage:
ModelAdmin | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => invalid


Comment:

`admin_order_field` doesn't support multiple columns as documented, you
can try to use `admin_order_field = Concat('issued__year',
'issued__month', 'number')`.

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

Django

unread,
Oct 17, 2020, 6:08:30 PM10/17/20
to django-...@googlegroups.com
#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns
-------------------------------------+-------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution: invalid
Keywords: admin_order_field, | Triage Stage:
ModelAdmin | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Petr Dlouhý):

@Mariusz Felisiak You cannot achieve the intended behaviour by that
`Concat`, because
a) I wanted to sort the number in reverse order
b) The length and content of the concatenated strings can vary.

I spend quite some time figuring up that concatenate function, until
finally I gave up.

So I think, this is valid use case for feature request, because the
concatenation is either not possible at all, or it would be quite complex
and very unreadable code.
I looked at the `ChangeList.get_ordering()` function, and since it returns
ordering list it might be quite simple code.

--
Ticket URL: <https://code.djangoproject.com/ticket/31975#comment:3>

Django

unread,
Oct 17, 2020, 6:22:00 PM10/17/20
to django-...@googlegroups.com
#31975: admin_order_field in ModelAdmin doesn't support sorting on multiple columns
-------------------------------------+-------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution:
Keywords: admin_order_field, | Triage Stage:
ModelAdmin | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petr Dlouhý):

* status: closed => new
* resolution: invalid =>


Comment:

I tried to modify the code of the `get_ordering` function, and I came with
following changed code (based on Django 3.0 code), that seems to be
working as expected (it is just the section in the `try` - `catch` block):


{{{
none, pfx, idx = p.rpartition('-')
field_name = self.list_display[int(idx)]
ordering_fields = self.get_ordering_field(field_name)
if not isinstance(ordering_fields, list):
ordering_fields = [ordering_fields]
for order_field in ordering_fields:
if not order_field:
continue # No 'admin_order_field', skip it
if hasattr(order_field, 'as_sql'):
# order_field is an expression.
ordering.append(order_field.desc() if pfx ==
'-' else order_field.asc())
# reverse order if order_field has already "-" as
prefix


elif order_field.startswith('-') and pfx == '-':

ordering.append(order_field[1:])
else:
ordering.append(pfx + order_field)

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31975#comment:4>

Django

unread,
Sep 14, 2021, 6:27:41 AM9/14/21
to django-...@googlegroups.com
#31975: Add support for list of fields to admin_order_field.
-------------------------------------+-------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution: wontfix
Keywords: admin_order_field, | Triage Stage:
ModelAdmin | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Petr Dlouhý):

I don't think, that is that niche, I even found Django snippet for this
created years ago: https://djangosnippets.org/snippets/10471/


On one hand it is small modification of Django code and small increase in
API complexity (which is consistent with the logic of other order fields,
BTW), on the other hand is many hours spend by programmers creating
unreadable Concat code in users applications and/or copying of large
portion of `get_queryset` function.

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

Reply all
Reply to author
Forward
0 new messages