skillfed

django-sortedm2m

Drop-in replacement for Django's many to many field with sorted relations.

django-sortedm2m v4.0.0 389.3K downloads/30d#7,032 on PyPI525
Permissive license BSD Active released

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-sortedm2m

uv

uv add django-sortedm2m

poetry

poetry add django-sortedm2m

Installing 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

Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: DjangoFramework :: Django :: 4.2Framework :: Django :: 5.0Framework :: Django :: 5.1Intended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

django ordered many to manydjango m2m with sort orderdjango sortedm2m fieldpreserve relation order djangodjango admin drag drop relations
django-ormmodel-fields

More Dynamic Content packages

MarkupSafe

MarkupSafe provides a text object that escapes…

permissive · top 100 on PyPI

Jinja2

Jinja2 is a templating engine that renders…

permissive · top 100 on PyPI

soupsieve

Soupsieve is a CSS selector library designed to…

permissive · top 100 on PyPI

Werkzeug

Werkzeug is a WSGI utility library providing…

permissive · top 1,000 on PyPI

Flask

Flask is a lightweight WSGI web application…

permissive · top 1,000 on PyPI

Mako

Mako compiles Python-embedded templates into…

permissive · top 1,000 on PyPI

django-more-admin-filters

Provides a collection of Django admin filter…

permissive · top 15,000 on PyPI

django-taggit

Adds simple tagging functionality to Django…

permissive · top 5,000 on PyPI

django-reverse-admin

Adds reverse inline support to Django admin for…

permissive · top 15,000 on PyPI

django-admin-sortable2

Adds drag-and-drop reordering of model…

permissive · top 5,000 on PyPI

django-ordered-model

Adds ordering and reordering capabilities to…

permissive · top 15,000 on PyPI

django-recurrence

Provides a Django model field and utilities for…

permissive · top 15,000 on PyPI

django-modelcluster

django-modelcluster extends Django models to…

permissive · top 5,000 on PyPI

django-admin-autocomplete-filter

Adds autocomplete-based filtering to Django…

copyleft · top 15,000 on PyPI

dj-inmemorystorage

An in-memory storage backend for Django that…

permissive · top 15,000 on PyPI

django-safedelete

Provides soft-delete functionality for Django…

permissive · top 5,000 on PyPI