Allows items of a collection to be manually sorted(such as for use with drag-and-drop).
Project description
Django Subjective Sort
This library enables subjective sorting of a list of objects. Imagine you are building a task management app, and the UI allows a user to manually sort his or her daily tasks via drag-and-drop. Django Subjective Sort makes it easy to handle re-positioning, both of the task that moved but also the other tasks that were affected by the move.
How it works
An abstract Django model class establishes an integer field named position
for storing the position of an object in a
list. Objects have no position by default (e.g. position = None
). Positioning is one-indexed with the object assigned
the 1
position having the highest prominence and objects without a position the lowest prominence. Objects cannot have
a position less than one, and cannot have a position greater than the number of objects in the list. If an object is
assigned a position greater than the number of objects with an assigned position, list objects without a position will
inherit one until the desired list position is reached.
Important: Never modify the value of the position
field directly. Only manage object positioning using the
reposition
method.
By design, Django Subjective Sort cannot influence list membership. It's only responsibility is managing the positioning of objects in the list it is provided. Since only a single position value is stored, each object can belong to only one subjectively sortable list. Using the task management example above, a task could be manually sorted among peers assigned to the same day, but not also among peers assigned to the same week or month.
Both for flexibility and because the Sortable model class is abstract, positioning changes are not automatically saved.
The example below demonstrates how to extend the reposition
method to save positioning changes for all objects
affected by re-positioning in a single transaction.
The Sortable model class contains two methods: reposition
and sort_by_position
. The reposition
method updates the
position of the object it is called on, as well as any of its list peers affected by the change. The sort_by_position
method is a convenience method for sorting a list of objects by their position.
Installation
$ pip install django-subjective-sort
Usage
- Extend the
Sortable
class to add custom sorting logic to any Django model.
# food/models.py
from django_subjective_sort.models import Sortable
class Food(Sortable):
pass
- Apply migrations.
$ python manage.py makemigrations
$ python manage.py migrate
- Extend the
reposition
method to save positioning changes for all objects affected by re-positioning in a single transaction.
# food/models.py
from typing import List
from src.django_subjective_sort.models import Sortable
class Food(Sortable):
# Extend the `Sortable` class to save sorting order.
# This allows flexibility to save other changes simultaneously.
def reposition(self,
peers: List['Food'],
position: int = None) -> List['Food']:
sortables_affected = super().reposition(peers, position) + [self]
# Save the changes
return Food.objects.bulk_update(sortables_affected, ['position'])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-subjective-sort-1.0.1.tar.gz
.
File metadata
- Download URL: django-subjective-sort-1.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a453cbd2bf63594cefa1e79a0c62e2321bf5f6ef41aeb8defdc9c40c129cf011 |
|
MD5 | aabe47981cb7f58f157742bf3a599355 |
|
BLAKE2b-256 | 3178718d43505becb4261cdb13de69b855799c3f98ea556c2b99502b36437bf7 |
File details
Details for the file django_subjective_sort-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_subjective_sort-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5399a19e5a406ceafb5e551d21d64af02a6a17423bb3261fef988dc5694fc4 |
|
MD5 | b291619d8ab777982e76d4d549ededbf |
|
BLAKE2b-256 | 38ea657dea15a5099635fa7b936604033408fd8de72c2cfdd00a389a79dd7dea |