jrief / django-admin-sortable2

Generic drag-and-drop ordering for objects in the Django admin interface
https://django-admin-sortable2.readthedocs.io/en/latest/
Other
770 stars 181 forks source link

Add support for ordering with `OrderBy` or `F` #368

Closed AndreyMZ closed 1 year ago

AndreyMZ commented 1 year ago

There are many way in Django to specify an ordering by column:

ASC DESC
str 'my_order' '-my_order'
F F('my_order')
OrderBy F('my_order').asc() F('my_order').desc()
OrderBy OrderBy(F('my_order')) OrderBy(F('my_order'), descending=True)

This PR adds support for all this ways. Now one can write:

class UpOrderedSortableBookAdmin(SortableBookAdmin):
    ordering = [F('my_order').asc()]

class DownOrderedSortableBookAdmin(SortableBookAdmin):
    ordering = [F('my_order').desc()]

Also custom ChangeList.get_ordering may return instances of OrderBy or F, not only str. Previously this resulted in e.g.:

AttributeError: 'OrderBy' object has no attribute 'rpartition'

For the rest minor changes see the commit messages.

jrief commented 1 year ago

Shall I release it as 2.2?

Thanks for this PR. Actually, I prefer smaller PRs which do not mix up fixes and features and version bumps, but I'm going to merge this anyway.

AndreyMZ commented 1 year ago

Shall I release it as 2.2?

I would appreciate it. Thank you!