models.py#

This module provides a model field that eases the implementation of reorderable items in Django’s admin interface. The ReorderItemsField is a PositiveSmallIntegerField build the ReorderItemsFieldMixin mixin.

This mixin makes the field known by the admin mixins in admin and the signal handlers in signals.

class reorder_items_widget.models.ReorderItemsFieldMixin(*args, grouped_by=None, **kwargs)#

Bases: object

This mixin makes an integer field known by the signal handlers and the admin classes as a reorder items field. (See signals and admin for more details.)

To work fine with the logic of the signal handlers we set unique=False, since it is not possible to update indexes of a unique field without running into unique contraint violations.

And since ReorderItemsModelAdminMixin excludes the reorder items field from the changeform we set blank=True to avoid validation errors. The value will be set by the pre_save signal handler when a new item is created.

There is an additional keyword argument grouped_by that can be used to specify a list of field names to group the items by. This is useful to make items reorderable in an inline context, where the order of the items should be specific to the related model. Here is a simple example to illustrate such a use case:

class Blog(models.Model):
    name = models.CharField(max_length=100)


class Article(models.Model):
    index = ReorderItemsField(grouped_by=['blog'])
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    text = models.TextField()

    class Meta:
        ordering = ('blog', 'index')
class reorder_items_widget.models.ReorderItemsField(*args, grouped_by=None, **kwargs)#

Bases: ReorderItemsFieldMixin, PositiveSmallIntegerField

A PositivSmallIntegerField using ReorderItemsFieldMixin.