mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #25790 -- Allowed disable column sorting in the admin changelist.
Thanks Ramiro Morales for completing the patch.
This commit is contained in:
parent
7d96f0c49a
commit
ef2512b2ff
8 changed files with 129 additions and 8 deletions
|
@ -1296,6 +1296,22 @@ subclass::
|
|||
a full count on the table which can be expensive if the table contains a
|
||||
large number of rows.
|
||||
|
||||
.. attribute:: ModelAdmin.sortable_by
|
||||
|
||||
.. versionadded:: 2.1
|
||||
|
||||
By default, the change list page allows sorting by all model fields (and
|
||||
callables that have the ``admin_order_field`` property) specified in
|
||||
:attr:`list_display`.
|
||||
|
||||
If you want to disable sorting for some columns, set ``sortable_by`` to
|
||||
a collection (e.g. ``list``, ``tuple``, or ``set``) of the subset of
|
||||
:attr:`list_display` that you want to be sortable. An empty collection
|
||||
disables sorting for all columns.
|
||||
|
||||
If you need to specify this list dynamically, implement a
|
||||
:meth:`~ModelAdmin.get_sortable_by` method instead.
|
||||
|
||||
.. attribute:: ModelAdmin.view_on_site
|
||||
|
||||
Set ``view_on_site`` to control whether or not to display the "View on site" link.
|
||||
|
@ -1564,6 +1580,24 @@ templates used by the :class:`ModelAdmin` views:
|
|||
to return the same kind of sequence type as for the
|
||||
:attr:`~ModelAdmin.search_fields` attribute.
|
||||
|
||||
.. method:: ModelAdmin.get_sortable_by(request)
|
||||
|
||||
.. versionadded:: 2.1
|
||||
|
||||
The ``get_sortable_by()`` method is passed the ``HttpRequest`` and is
|
||||
expected to return a collection (e.g. ``list``, ``tuple``, or ``set``) of
|
||||
field names that will be sortable in the change list page.
|
||||
|
||||
Its default implementation returns :attr:`sortable_by` if it's set,
|
||||
otherwise it defers to :meth:`get_list_display`.
|
||||
|
||||
For example, to prevent one or more columns from being sortable::
|
||||
|
||||
class PersonAdmin(admin.ModelAdmin):
|
||||
|
||||
def get_sortable_by(self, request):
|
||||
return {*self.get_list_display(request)} - {'rank'}
|
||||
|
||||
.. method:: ModelAdmin.get_inline_instances(request, obj=None)
|
||||
|
||||
The ``get_inline_instances`` method is given the ``HttpRequest`` and the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue