This project makes it easy to add drag-and-drop ordering to any model in
Django admin. Inlines for a sortable model may also be made sortable,
enabling individual items or groups of items to be sortable.
=======
## Supported Django Versions
If you're using Django 1.4.x, use django-admin-sortable 1.4.9 or below.
For Django 1.5.x, use the latest version of django-admin-sortable.
django-admin-sortable 1.5.2 introduced backward-incompatible changes for Django 1.4.x
## Installation
1. pip install django-admin-sortable
--or--
Download django-admin-sortable from [source](https://github.com/iambrandontaylor/django-admin-sortable/archive/master.zip)
1. Unzip the directory and cd into the uncompressed project directory
2. *Optional: Enable your virtualenv
3. Run `$ python setup.py install` or add `adminsortable` to your PYTHONPATH.
## Configuration
1. Add `adminsortable` to your `INSTALLED_APPS`.
2. Ensure `django.core.context_processors.static` is in your `TEMPLATE_CONTEXT_PROCESSORS`.
### Static Media
Preferred:
Use the [staticfiles app](https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/)
Alternate:
Copy the `adminsortable` folder from the `static` folder to the
location you serve static files from.
### Testing
Have a look at the included sample_project to see working examples.
The login credentials for admin are: admin/admin
When a model is sortable, a tool-area link will be added that says "Change Order".
Click this link, and you will be taken to the custom view where you can drag-and-drop
the records into order.
Inlines may be drag-and-dropped into any order directly from the change form.
## Usage
### Models
To add sorting to a model, your model needs to inherit from `Sortable` and
have an inner Meta class that inherits from `Sortable.Meta`
#models.py
from adminsortable.models import Sortable
class MySortableClass(Sortable):
class Meta(Sortable.Meta):
pass
title = models.CharField(max_length=50)
def __unicode__(self):
return self.title
It is also possible to order objects relative to another object that is a ForeignKey,
even if that model does not inherit from Sortable:
from adminsortable.fields import SortableForeignKey
#models.py
class Category(models.Model):
title = models.CharField(max_length=50)
...
class Project(Sortable):
class Meta(Sortable.Meta):
pass
category = SortableForeignKey(Category)
title = models.CharField(max_length=50)
def __unicode__(self):
return self.title
Sortable has one field: `order` and adds a default ordering value set to `order`.
### Adding Sortable to an existing model
If you're adding Sorting to an existing model, it is recommended that you use [django-south](http://south.areacode.com/) to create a schema migration to add the "order" field to your model. You will also need to create a data migration in order to add the appropriate values for the `order` column.
Example assuming a model named "Category":
def forwards(self, orm):
for index, category in enumerate(orm.Category.objects.all()):
category.order = index + 1
category.save()
See: [this link](http://south.readthedocs.org/en/latest/tutorial/part3.html) for more
information on Data Migrations.
### Django Admin Integration
To enable sorting in the admin, you need to inherit from `SortableAdmin`:
Because of the way inline models are added to their parent model in the
change form, it is not currently possible to have sortable inline models
whose parent does not inhert from `Sortable`. A workaround is currently
being investigated.
### Rationale
Other projects have added drag-and-drop ordering to the ChangeList
view, however this introduces a couple of problems...
- The ChangeList view supports pagination, which makes drag-and-drop
ordering across pages impossible.
- The ChangeList view by default, does not order records based on a
foreign key, nor distinguish between rows that are associated with a
foreign key. This makes ordering the records grouped by a foreign key
impossible.
- The ChangeList supports in-line editing, and adding drag-and-drop
ordering on top of that just seemed a little much in my opinion.
### Status
django-admin-sortable is currently used in production.
### What's new in 1.5.5?
- Improved url resolution to sorting urls
- Fixed a potential issue with JavaScript namespace on sortable() calls that could prevent sortable from working in environments where jQuery is already included, such as Django-CMS
### Future
- Support for foreign keys that are self referential
- Move unit tests out of sample project (I could really use some help with this one)
- Travis CI integration
### License
django-admin-sortable is released under the Apache Public License v2.