django-sortedm2m
Drop-in replacement for Django's many to many field with sorted relations.
What it is and what it does
django-sortedm2m is a Django field that works exactly like the built-in ManyToManyField but remembers the order in which you add related objects. Instead of getting relations back in an arbitrary order, you get them in the order you added them, with the sort position stored in the database. This is useful for galleries, playlists, or any scenario where the sequence of related items matters.
The package provides a SortedManyToManyField class you drop into your models in place of ManyToManyField, and an optional admin widget that lets you reorder relations by dragging and dropping. It handles database migrations automatically and supports Django 4.2 through 5.1 on Python 3.7–3.12. There are no runtime dependencies beyond Django itself.
Use it for:
- Gallery or portfolio models where photos must appear in a specific sequence chosen by the user.
- Playlist or media collection models where track or item order is part of the data model.
- Admin interfaces where users need to reorder many-to-many relationships via drag-and-drop.
- Legacy database integration where you need to rename the sort field to match existing schema.
- Custom through models where you want to add a __str__ method to improve admin display of relationships.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides a drop-in replacement for Django's ManyToManyField that preserves the order of related objects, storing sort order in the database and exposing it through a custom admin widget.
Yes. This is a well-maintained, production-stable package with no known vulnerabilities, low install friction, and a permissive license. It solves a real Django limitation (unordered many-to-many relations) with a straightforward drop-in replacement. Use it whenever you need to preserve relation order in a Django model.
Install
django-sortedm2m on PyPI
pip
pip install django-sortedm2muv
uv add django-sortedm2mpoetry
poetry add django-sortedm2mInstalling django-sortedm2m
Before you install
Low friction installation with no runtime dependencies. Actively maintained with recent releases supporting Django 5.1 and Python 3.7–3.12; repository shows steady activity and 525 stars.
License in practice
BSD license is permissive; you can use, modify, and distribute this package with minimal restrictions, provided you include the license notice.
Quickstart
pip install django-sortedm2m
from sortedm2m.fields import SortedManyToManyField
from django.db import models
class Gallery(models.Model):
photos = SortedManyToManyField('Photo')
gallery = Gallery.objects.create(name='My Gallery')
for photo in Photo.objects.order_by('name'):
gallery.photos.add(photo) # order is preserved
Requires Django 4.2, 5.0, or 5.1; add 'sortedm2m' to INSTALLED_APPS to use the admin widget and its CSS/JS assets.
Verify before relying
- Whether the package supports Django versions earlier than 4.2 (version 4.0.0 dropped support for outdated versions, but exact cutoff is unclear).
- Performance characteristics when managing large numbers of related objects or complex sort operations.
Package facts
| License | BSD (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 733 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 389,270/month — #7,032 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_sortedm2m-4.0.0-py2.py3-none-any.whl
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
django-more-admin-filtersProvides a collection of Django admin filter…
permissive · top 15,000 on PyPI
django-taggitAdds simple tagging functionality to Django…
permissive · top 5,000 on PyPI
django-reverse-adminAdds reverse inline support to Django admin for…
permissive · top 15,000 on PyPI
django-admin-sortable2Adds drag-and-drop reordering of model…
permissive · top 5,000 on PyPI
django-ordered-modelAdds ordering and reordering capabilities to…
permissive · top 15,000 on PyPI
django-recurrenceProvides a Django model field and utilities for…
permissive · top 15,000 on PyPI
django-modelclusterdjango-modelcluster extends Django models to…
permissive · top 5,000 on PyPI
django-admin-autocomplete-filterAdds autocomplete-based filtering to Django…
copyleft · top 15,000 on PyPI
dj-inmemorystorageAn in-memory storage backend for Django that…
permissive · top 15,000 on PyPI
django-safedeleteProvides soft-delete functionality for Django…
permissive · top 5,000 on PyPI