django-admin-sortable2 is a Django package that provides generic drag-and-drop ordering functionality for objects in the List, Stacked-Inline, and Tabular-Inline Views of the Django Admin interface. It enriches existing `ModelAdmin` and `InlineModelAdmin` classes with sortable behavior via mixins. The library is actively maintained, with frequent minor and patch releases to support newer Django and Python versions.
pip install django-admin-sortable2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to make both a main ModelAdmin list view and its associated inline forms sortable. It requires adding `adminsortable2` to `INSTALLED_APPS`, defining a positive integer `order` field (with `default=0` and in `Meta.ordering`) in your models, and then applying `SortableAdminMixin` to your `ModelAdmin` and `SortableTabularInline` (or `SortableStackedInline`) to your inline admin classes. Remember to run `makemigrations` and `migrate` after defining your models.
Review the official documentation for migration guidelines when upgrading from 1.x. Ensure your Django version is 4.2 or higher, and Python is 3.9 or higher.
Ensure your sortable model has `order = models.PositiveIntegerField(default=0, blank=False, null=False, db_index=True)` and `class Meta: ordering = ['order']`.
After adding the order field, run the management command `./manage.py reorder <app_label.ModelName>` to automatically populate the `order` field for all existing instances.
Inform users that they must click the 'Save' button on the parent model's detail page after reordering inline items.
Update your custom inline formset class to inherit from `CustomInlineFormSet`, e.g., `class MyCustomFormSet(CustomInlineFormSet, BaseInlineFormSet): ...`