matthiask / django-admin-ordering

Drag-drop orderable change lists and inlines done right.
BSD 3-Clause "New" or "Revised" License
64 stars 7 forks source link

============================================================================== django-admin-ordering -- Orderable change lists and inlines done right^Wsimple

.. image:: https://github.com/matthiask/django-admin-ordering/actions/workflows/tests.yml/badge.svg :target: https://github.com/matthiask/django-admin-ordering/ :alt: CI Status

Please refer to the CI build linked above for the currently supported combinations of Python and Django.

Installation

pip install django-admin-ordering, and add admin_ordering to INSTALLED_APPS.

Usage

First, you need a model ordered by an integer field. If you are happy with a model where 1. the ordering field is called ordering and 2. the ordering field is automatically initialized so that new objects are ordered last you can also inherit the abstract admin_ordering.models.OrderableModel model. If you define your own class Meta you should inherit OrderableModel.Meta so that the ordering attribute is set to the correct value:

.. code-block:: python

from admin_ordering.models import OrderableModel

class MyModel(OrderableModel):
    # ...

    class Meta(OrderableModel.Meta):
        # ...

Orderable change lists


.. code-block:: python

    from admin_ordering.admin import OrderableAdmin

    @admin.register(MyModel)
    class MyModelAdmin(OrderableAdmin, admin.ModelAdmin):
        # The field used for ordering. Prepend a minus for reverse
        # ordering: "-ordering"
        ordering_field = "ordering"  # The default.

        # You may optionally hide the ordering field in the changelist:
        # ordering_field_hide_input = False

        # The ordering field must be included both in list_display and
        # list_editable:
        list_display = ["name", "ordering"]
        list_editable = ["ordering"]

Orderable inlines

.. code-block:: python

from admin_ordering.admin import OrderableAdmin

class MyModelTabularInline(OrderableAdmin, admin.TabularInline):
    model = MyModel

    # Same as above; "-ordering" is also allowed here:
    ordering_field = "ordering"
    # ordering_field_hide_input = False

OrderableAdmin comes with a default of extra = 0 (no extra empty inlines shown by default). It is strongly recommended to leave the changed default as-is, because otherwise you'll end up with invalid inlines just because you wanted to change the ordering.

Limitations