Django Sorting Field
Project description
This package implements a Django form field + widget for drag & drog sorting of items
Sorting any item with a field called id is supported
The drag and drop feature has been implemented with html5sortable.
Known limitations
Refreshing the items on the widget not (yet?) supported out of the box
No tests
Example of the widget
Usage
The sort order field should be implemented on the model containing the sorted objects. This allows ordering of different instances of the same item set differently.
Let’s say you have image CarouselPlugin, Carousel, and Picture models, and you wish to be able to sort the same Carousel instance differently on each CarouselPlugin.
You also have a CMSPlugin object for the carousel.
class Carousel(models.Model):
pass
class Picture(models.Model):
carousel = models.ForeignKey(Carousel, related_name="pictures")
image = SomeImageField()
name = models.CharField()
class CarouselPlugin(CMSPlugin):
carousel = models.ForeignKey(Carousel, related_name="x")
class CMSCarouselPlugin(CMSPluginBase):
model = CarouselPlugin
def render(self, context, instance, placeholder):
context.update({
"pictures": self.instance.carousel.pictures.all(),
})
return context
Achieving the wanted behavior can be done in the following steps:
Add a (nullable) TextField to the model containing the order information
class CarouselPlugin(CMSPlugin):
carousel = models.ForeignKey(Carousel, related_name="x")
carousel_order = models.TextField(null=True)
Add the SortingFormField to the CMS Plugin and populate it
from django_sorting_field.fields import SortingFormField
class CarouselPluginForm(forms.ModelForm):
carousel_order = SortingFormField()
def __init__(self, *args, **kwargs):
super(CarouselPluginForm, self).__init__(*args, **kwargs)
if self.instance.pk:
self.fields["carousel_order"].populate(
items=self.instance.carousel.pictures.all(),
)
class CMSCarouselPlugin(CMSPluginBase):
model = CarouselPlugin
form = CarouselPluginForm
def render(self, context, instance, placeholder):
context.update({
"pictures": self.instance.carousel.pictures.all(),
})
return context
Finally, sort the items passed to the context data
from django_sorting_field.utils import sort_by_order
class CMSCarouselPlugin(CMSPluginBase):
model = CarouselPlugin
form = CarouselPluginForm
def render(self, context, instance, placeholder):
context.update({
"pictures": sort_by_order(
self.instance.carousel.pictures.all(),
self.instance.carousel_order
),
})
return context
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-sorting-field-1.0.2.tar.gz
.
File metadata
- Download URL: django-sorting-field-1.0.2.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e791dd6cf1deab565888875a88f05384f42ac8dfcae92562da82582a0c7dfaf9 |
|
MD5 | ce4bf5c73003b786345b6e306331696f |
|
BLAKE2b-256 | 5db112d7a469b47f03869de02ca0db8e17bf15ebf68e194438c6346b7a76f0ba |
File details
Details for the file django_sorting_field-1.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_sorting_field-1.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baf750e2fd7267c0326877bf2ca8bcb99392ba75a63dd6f3f4b4512695698337 |
|
MD5 | 62c04d3f20a5d69acd3884f815281ffd |
|
BLAKE2b-256 | 7195d2f693c891cad708defd8cfca7613d3e4dd099381ccdca7ce188b668f83b |