django-admin-sortable2

This Django module is as a replacement for django-admin-sortable using an unintrusive approach.

It is a generic drag-and-drop ordering module for sorting objects in the list view of the Django admin interface. This plugin offers simple mixin classes which enrich the functionality of any existing class derived from admin.ModelAdmin, admin.StackedInline or admin.TabluarInline. It thus makes it very easy to integrate with existing models and their model admin interfaces.

Project home: https://github.com/jrief/django-admin-sortable2

Please ask questions and report bugs on: https://github.com/jrief/django-admin-sortable2/issues

Why another adminsortable plugin?

All available plugins which add functionality to make list views for the Django admin interface sortable, offer a base class to be used instead of models.Model. This abstract base class then contains a hard coded position field, additional methods, and meta directives. The problem with such an approach is twofold:

First, an is-a relationship is abused to enrich the functionality of a class. This is bad OOP practice. A base class shall only be used to reflect a real is-a relation which specializes this class. For instance: A mammal is an animal, a primate is a mammal, homo sapiens is a primate, etc. Here the inheritance model is appropriate, but it would be wrong to derive from homo sapiens to reflect a human which is able to hunt using bows and arrows.

Therefore, a sortable model is not an unsortable model. Making a Django Model sortable enriches its functionality. In OOP design this does not qualify for an is-a relationship.

Fortunately, Python makes it very easy to distinguish between real is-a relationships and interfaces enriching their functionality. The latter are handled by so named mixin classes. They offer additional methods for a class without deriving from the base class.

Also consider the case when someone wants to augment some other functionality of a model class. If he also derives from models.Model, he would create another abstract intermediate class. Someone who wants to use both functional augmentations, now is in trouble. He has to choose between one of the two, as he cannot derive from both of them, because then its model class would inherit the base class models.Model twice. This kind of diamond-shaped inheritance is to be avoided under all circumstances.

By using a mixin class rather than deriving from a special abstract base class, these problems can be avoided!

Contents:

Installation

Install django-admin-sortable2. The latest stable release can be found on PyPI

pip install django-admin-sortable2

or the newest development version from GitHub

pip install -e git+https://github.com/jrief/django-admin-sortable2#egg=django-admin-sortable2

Configuration

Add 'adminsortable2' to the list of INSTALLED_APPS in your project’s settings.py file

INSTALLED_APPS = (
    ...
    'adminsortable2',
    ...
)

Using Admin Sortable

This Django module offers three mixin classes to be added to the existing classes of your model admin:

  • admin.ModelAdmin
  • admin.StackedInline
  • admin.TabularInline

They slightly modify the admin views of a sortable model. There is no need to derive your model class from a special base model class. You can use your existing ordered field, just as you always did, or add a new one with any name you like, if needed.

Integrate your models

Each database model which shall be sortable, requires a position value in its model description. Rather than defining a base class, which contains such a positional value in a hard coded field, this module lets you reuse existing sort fields or define a new field for the sort value.

Therefore this module can be applied in situations where your model is derived from an existing abstract model which already contains any kind of position value. The only requirement for this module is, that this position value be specified as the primary field used for sorting. This in Django is declared through the model’s Meta class. An example models.py:

class SortableBook(models.Model):
    title = models.CharField('Title', null=True, blank=True, max_length=255)
    my_order = models.PositiveIntegerField(default=0, blank=False, null=False)

    class Meta(object):
        ordering = ('my_order',)

Here the ordering field is named my_order, but you may choose any other name. There are only two constraints:

  • my_order is the first field in the ordering tuple of the model’s Meta class.

  • my_order‘s default value must be 0. The JavaScript which performs the sorting is 1-indexed,

    so this will not interfere with the order of your items, even if you’re already using 0-indexed ordering fields.

The field used to store the ordering position may be any kind of numeric model field offered by Django. Use one of these models fields:

  • models.BigIntegerField
  • models.IntegerField
  • models.PositiveIntegerField (recommended)
  • models.PositiveSmallIntegerField (recommended for small sets)
  • models.SmallIntegerField

Additionally you may use models.DecimalField or models.FloatField, but these model fields are not recommended.

Warning

Do not make this field unique! See below why.

In Django’s Admin, make the list view sortable

Next to the action checkbox, a draggable area is added to each entry line. The user than may click on any item and vertically drag that item to a new position.

Sortable List View
Sortable List View

If one or more items shall be moved to another page, this can easily been done by selecting them though the action checkbox. Then the user shall click on a predefined action from the pull down menu on the top of the list view.

Integrate into a list view

In admin.py, add a mixin class to augment the functionality for sorting (be sure to put the mixin class before model.ModelAdmin):

from django.contrib import admin
from adminsortable2.admin import SortableAdminMixin
from models import MyModel

class MyModelAdmin(SortableAdminMixin, admin.ModelAdmin):
    pass
admin.site.register(MyModel, MyModelAdmin)

That’s it! The list view of the model admin interface now adds a column with a sensitive area. By clicking on that area, the user can move that row up or down. If he wants to move it to another page, he can do that as a bulk operation, using the admin actions.

Make a stacked or tabular inline view sortable

The interface for a sortable stacked inline view looks exactly the same. If you click on an stacked inline’s field title, this whole inline form can be moved up and down.

The interface for a sortable tabular inline view adds a sensitive area to each draggable row. These rows then can be moved up and down.

Sortable Tabular Inlines
Sortable Tabular Inlines

After moving a tabular or stacked inline, save the model form to persist its sorting order.

Integrate into a detail view
from django.contrib import admin
from adminsortable2.admin import SortableInlineAdminMixin
from models import MySubModel, MyModel

class MySubModelInline(SortableInlineAdminMixin, admin.TabularInline):  # or admin.StackedInline
    model = MySubModel

class MyModelAdmin(admin.ModelAdmin):
    inlines = (MySubModelInline,)
admin.site.register(MyModel, MyModelAdmin)

Initial data

In case you just changed your model to contain an additional sorting field (e.g. my_order), which does not yet contain any values, then you must set initial ordering values.

django-admin-sortable2 is shipping with a management command which can be used to prepopulate the ordering field:

shell> ./manage.py reorder my_app.models.MyModel

If you prefer to do a one-time database migration, just after having added the ordering field to the model, then create a datamigration:

shell> ./manage.py datamigration myapp preset_order

this creates an empty migration named something like migrations/0123_preset_order.py. Edit the file and change it into a data migration:

class Migration(DataMigration):
    def forwards(self, orm):
        order = 0
        for obj in orm.MyModel.objects.all():
            order += 1
            obj.my_order = order
            obj.save()

then apply the changes to the database using:

shell> ./manage.py migrate myapp

Note

If you omit to prepopulate the ordering field with unique values, after adding this field to an existing model, then attempting to reorder field manually will fail.

Note on unique indices on the position field

From a design consideration, one might be tempted to add a unique index on the ordering field. But in practice this has serious drawbacks:

MySQL has a feature (or bug?) which requires to use the ORDER BY clause in bulk updates on unique fields.

SQLite has the same bug which is even worse, because it does neither update all the fields in one transaction, nor does it allow to use the ORDER BY clause in bulk updates.

Only PostgreSQL does it “right” in the sense, that it updates all fields in one transaction and afterwards rebuilds the unique index. Here one can not use the ORDER BY clause during updates, which from the point of view for SQL semantics, is senseless anyway.

See https://code.djangoproject.com/ticket/20708 for details.

Therefore I strongly advise against setting unique=True on the position field, unless you want unportable code, which only works with Postgres databases.

Run Example Code

To get a quick first impression of this plugin, clone this repositoty from GitHub and run an example webserver:

git clone https://github.com/jrief/django-admin-sortable2.git
cd django-admin-sortable2/example/
./manage.py syncdb
# add an admin user
./manage.py loaddata testapp/fixtures/data.json
./manage.py runserver

Point a browser onto http://localhost:8000/admin/, log in and go to Sortable books. There you can test the behavior of this Django app.

Release history

0.5.0

  • Changed the namespace from adminsortable to adminsortable2 to allow both this

project and django-admin-sortable to co-exist in the same project. This is helpful for projects to transition from one to the other library. It also allows existing projects’s migrations which previously relied on django-admin-sortable to continue to work.

0.3.2

  • Fixed #42: Sorting does not work when ordering is descending.

0.3.2

  • Using property method media() instead of hard coded Media class.
  • Using the verbose_name from the column used to keep the order of fields instead of a hard coded “Sort”.
  • When updating order in change_list_view, use the CSRF protection token.

0.3.1

  • Fixed issue #25: admin.TabularInline problem in django 1.5.x
  • Fixed problem when adding new Inline Form Fields.
  • PEP8 cleanup.

0.3.0

  • Support for Python-3.3.
  • Fixed: Add list-sortable.js on changelist only. Issue #31.

0.2.9

  • Fixed: StackedInlines do not add an empty field after saving the model.
  • Added management command to preset initial ordering.

0.2.8

  • Refactored documentation for Read-The-Docs

0.2.7

  • Fixed: MethodType takes only two attributes

0.2.6

  • Fixed: Unsortable inline models become draggable when there is a sortable inline model

0.2.5

  • Bulk actions are added only when they make sense.
  • Fixed bug when clicking on table header for ordering field.

0.2.4

  • Fix CustomInlineFormSet to allow customization. Thanks yakky.

0.2.2

  • Distinction between different versions of jQuery in case django-cms is installed side by side.

0.2.0

  • Added sortable stacked and tabular inlines.

0.1.2

  • Fixed: All field names other than “order” are now allowed.

0.1.1

  • Fixed compatibility issue when used together with django-cms.

0.1.0

  • First version published on PyPI.

0.0.1

First working release.

Indices and tables

License

Copyright © 2014 Jacob Rief. Licensed under the MIT license.