Django Admin Sorting Module




In this post, we’ll learn how to add drag-and-drop sorting to the Django Admin interface using the django-admin-sortable2 package.

Official Documentation: https://django-admin-sortable2.readthedocs.io/en/latest/installation.html



 



Quick Setup
 

Optionally, you can  clone a basic Django project mysite from my GitHub(38130)

git clone https://github.com/38130/mysite

Alternatively, assume the following project structure:

mysite/
mysite/
settings.py
polls/
admin.py
models.py
manage.py

 


Installation
 

pip install django-admin-sortable2


Add the app to INSTALLED_APPS

INSTALLED_APPS = [
    ...
    'adminsortable2',
]

 


 

Create a sortable model
 

from django.db import models

class Book(models.Model):

    title = models.CharField(max_length=255)
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['order']


Apply the migrations

python manage.py makemigrations
python manage.py migrate

 

 

Admin integration
 

from django.contrib import admin
from .models import Book
from adminsortable2.admin import SortableAdminMixin

@admin.register(Book)
class BookAdmin(SortableAdminMixin, admin.ModelAdmin):
    pass

Through http://localhost:8000/admin/polls/book/, add some instances to the model and it should be working right away!





Final thoughts

django-admin-sortable2 provides a simple and powerful way to add manual ordering to your Django Admin.
With just a few lines of code, you can turn static lists into interactive, user-friendly interfaces.


Tested with:
Django==5.2
django-admin-sortable2==2.2.8