django-orderedmodel
-- orderable models for Django
django-orderedmodel
helps you create Django models that can be
moved up/down with respect to each other.
There are a few simple steps to follow to make your models orderable:
- Install
django-orderedmodel
using your favourite way of installing Django packages. - Add
'orderedmodel'
toINSTALLED_APPS
in yoursettings.py
. - Ensure that your project is using
django.contrib.staticfiles
to serve static content (this is now required for compatibility with Django 1.4+). - Derive your Model from
orderedmodel.OrderedModel
. - Derive your ModelAdmin from
orderedmodel.OrderedModelAdmin
. - Add
reorder
field to yout ModelAdmin'slist_display
. - Enjoy!
Now you can use order_by('order')
in your query to get instances of your model
in desired order (actually it is not neccessary to call order_by
explicitly
unless you have changed default ordering in your model's Meta).
Suppose you have a django app called testapp.
You need an orderable model TestModel
.
models.py:
from django.db import models
from orderedmodel import OrderedModel
class TestModel(OrderedModel):
name = models.CharField(max_length=30)
admin.py:
from django.contrib import admin
from orderedmodel import OrderedModelAdmin
from testapp.models import TestModel
class TestModelAdmin(OrderedModelAdmin):
list_display = ['name', 'reorder']
admin.site.register(TestModel, TestModelAdmin)
Yep! Now if you create several instances of your model and look into admin site you'll see something like this:
Current version of django-orderedmodel
requires Django-1.6+.
See branch django-1.3
for version compatible with Django-1.3.
See branch django-1.4
for version compatible with Django-1.4.
Sure!