Package: django-admin-sort
django-admin-sortSortable changelist, tabular and stacked inlines. Using existing order fields, flexible.Originally based onjrief's django-admin-sortable2,
django-admin-sort tries to further simplify, but also add some minor new features (like dropdown sortables, someday).django-admin-sort's focus is on admin sorting, as the name suggests. Nevertheless, it provides a very simpleSortableModelMixinclass, that can be used to add sorting on your models, without the admin.InstallationThe latest stable release can be found on PyPI.pipinstalldjango-admin-sortAdd'admin_sort'to the list ofINSTALLED_APPSin your project'ssettings.pyfile.INSTALLED_APPS=(..,'admin_sort',)Using Admin SortThis Django module offers two mixin classes to be added to the existing classes of your model
admin:admin_sort.admin.SortableAdminMixinadmin_sort.admin.SortableInlineAdminMixinThey slightly modify the admin views of a sortable model. There is no need to derive your model
class from a special base model class. But if you want (or if you dont need the admin), you can
use theadmin_sort.models.SortableModelMixin, a convinience mixin to make your model sortable.Integrate your modelsEach database model which shall be sortable, requires a position value in its model description.classSortableBook(models.Model):title=models.CharField('Title',null=True,blank=True,max_length=255,)my_order=models.PositiveIntegerField(default=0,blank=False,null=False,)classMeta(object):ordering=('my_order','title',)Here the ordering field is namedmy_order, but you may choose any other name. One constraint: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.PositiveIntegerFieldmodels.PositiveSmallIntegerField(recommended for small sets)WARNING:Do not make this field unique!Sortable list viewInadmin.py, add a mixin class to augment the functionality for sorting (be sure to put the
mixin class before model.ModelAdmin):fromdjango.contribimportadminfromadmin_sort.adminimportSortableAdminMixinfrommodelsimportMyModelclassMyModelAdmin(SortableAdminMixin,admin.ModelAdmin):position_field='my_order'# requiredinsert_position='first|last'# optional, last is defaultadmin.site.register(MyModel,MyModelAdmin)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.Sortable stacked or tabular inlinefromdjango.contribimportadminfromadmin_sort.adminimportSortableInlineAdminMixinfrommodelsimportMySubModel,MyModelclassMySubModelInline(SortableInlineAdminMixin,admin.TabularInline):# or admin.StackedInlinemodel=MySubModelclassMyModelAdmin(admin.ModelAdmin):inlines=(MySubModelInline,)admin.site.register(MyModel,MyModelAdmin)The interface for a sortable stacked inline view is similar. 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.After moving a tabular or stacked inline, save the model form to persist
its sorting order.Initial datadjango-admin-sort adds a "reorder" button in the admin change list (just next to "add new"), for superadmins only.
Hit it, and the position_field will be repopulated, ensuring data integrity.LicenseCopyright © 2018 Alaric Mägerle & Ben Stähli
Licensed under the MIT license.Run Example CodeTo get a quick first impression of this plugin, clone this repositoty
from GitHub and run an example webserver:.. code:: bashgitclonehttps://github.com/rouxcode/django-admin-sort.gitcddjango-admin-sort/example/
./manage.pysyncdb
./manage.pycreatesuperuser
./manage.pyloaddatatestapp/fixtures/data.json
./manage.pyrunserverPoint a browser ontohttp://localhost:8000/admin/, log in and go toSortable books. There you can
test the behavior of this Django app.geckodriver installvisithttps://github.com/mozilla/geckodriver/releasesdownload the latest version of "geckodriver-vX.XX.X-linux64.tar.gz"unarchive the tarball (tar -xvzf geckodriver-vX.XX.X-linux64.tar.gz)give executable permissions to geckodriver (chmod +x geckodriver)move the geckodriver binary to /usr/local/bin or any location on your system PATH.