Skip to main content

Natural sorting for Django and Django REST Framework

Project description

Django Natural Sort

PyPI PyPI - License PyPI - Python Version PyPI - Django Version

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.ordering 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_natural_sort-0.1.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_natural_sort-0.1.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file django_natural_sort-0.1.0.tar.gz.

File metadata

  • Download URL: django_natural_sort-0.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.0

File hashes

Hashes for django_natural_sort-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b02a8bc20d514da182b6887c77d2a1c31ffc2afe308184a7324fd1f58c0d32d6
MD5 2dcd5d8841fbd8829e8317ea9abcc4cd
BLAKE2b-256 876aae0d559ed44a37141965f122e44b7f3e1f34419696ad7a2a08987690c59e

See more details on using hashes here.

File details

Details for the file django_natural_sort-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_natural_sort-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa97a39dc11d74d67c8c089c7766e50304cf153fa24c7815d60180920d844781
MD5 78f26db1451c4b13167d3c3337dfabbd
BLAKE2b-256 4f7e09c9e840c88141d399448b4339c1a5ac8f779735c0cc97318acc18a55003

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page