--- id: django-sortedm2m version: "4.0.0" license: BSD license_treatment: permissive maintenance: active --- # django-sortedm2m — Drop-in replacement for Django's many to many field with sorted relations. License: permissive · Maintenance: active · Downloads: 389.3K/mo ## 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 above — 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 pip install django-sortedm2m uv add django-sortedm2m 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: unspecified - Install friction: low - Maintenance: active - Downloads: 389.3K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags django ordered many to many, django m2m with sort order, django sortedm2m field, preserve relation order django, django admin drag drop relations, django-orm, model-fields [View on SkillFed](https://skillfed.io/packages/django-sortedm2m) · [View on PyPI](https://pypi.org/project/django-sortedm2m/)