Django Natural Sort
A lightweight, efficient package for natural sorting in Django and Django REST Framework.
The Problem
By default, when you order strings containing numbers in Django or DRF, they're sorted lexicographically:
['1', '10', '11', '12', '2', '3', '4', '5', '6', '7', '8', '9']
This is not the expected order for humans, who would expect:
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
This package solves this issue by implementing natural sorting, where numbers within strings are treated as actual numbers rather than individual characters.
Installation
pip install django-natural-sort
Usage
In Django REST Framework Views
from django_natural_sort.filters import NaturalOrderingFilter
class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
filter_backends = [NaturalOrderingFilter]
ordering_fields = ['id', 'name', 'version']
This automatically applies natural sorting to any CharField, EmailField, or TextField in your model when used in ordering. Just use the standard ordering parameter:
GET /api/mymodel/?ordering=version
Features
- Efficient: Uses database ordering for non-string fields, only falls back to Python sorting when necessary
- Compatible: Works with Django 3.2+ and Django REST Framework 3.11.1+
- Lightweight: No external dependencies
- Flexible: Works with various string field types (CharField, TextField, EmailField)
- Robust: Handles edge cases like mixed types, None values, and more
How It Works
The package detects which fields in your model are string fields. When ordering by these fields, it uses a natural sorting algorithm that correctly handles numbers embedded in strings.
For non-string fields, it uses the database's built-in ordering mechanism for optimal performance.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for django-natural-sort 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_natural_sort-0.1.1.tar.gz | 11.0 kB | Without provenance | Details for django_natural_sort-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_natural_sort-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for django_natural_sort-0.1.1-py3-none-any.whl |
Total release size: 23.6 kB
Release files / django_natural_sort-0.1.1.tar.gz
| Download URL | django_natural_sort-0.1.1.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c64624578ed8ad033786401525b04ca21e0ef920c7bdc2488a1e0f857ed640af
|
|
BLAKE2b-256 checksum How to use checksums |
aa6f8719c705439c14262097293a10285a2d91a4f93bc81c00a6f826939486ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.0
|
Release files / django_natural_sort-0.1.1-py3-none-any.whl
| Download URL | django_natural_sort-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b17a0eaebd74571d269e10449d57f0e4459f52b6bc0ac939258acb684c720b3
|
|
BLAKE2b-256 checksum How to use checksums |
d3859c85d82cadb5655a2f1a9044ba29cae230a363fc3116d2cb49b1cebcabdb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.0
|
Security for django-natural-sort 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.