django-orderedmodel
-- orderable models for Django
django-orderedmodel
intends to help you create Django models which can be
moved up\down (or left\right) with respect to each other.
This is a clone of original django-ordermodel, with added new features
There are a few simple steps to follow to make your models orderable:
- Add django-ordered model to your enviroment
- With pip
pip install git+git://github.com/IndustriaTech/django-orderedmodel.git
.
- Without pip
git clone git://github.com/IndustriaTech/django-orderedmodel.git
.- Copy (or, even better, symlink)
orderedmodel
directory to your Django project.
- Add
'orderedmodel'
toINSTALLED_APPS
in yoursettings.py
. - Ensure that your project is using
django.contrib.staticfiles
to serve static content - Derive your Model from
orderedmodel.models.OrderedModel
. - Derive your ModelAdmin from
orderedmodel.models.OrderedModelAdmin
. - 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.models import OrderedModel
class TestModel(OrderedModel):
name = models.CharField(max_length=30)
admin.py:
from django.contrib import admin
from orderedmodel.admin 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.4+.
Now there is a basic supoort of django-mptt. If you want to make simple reordering in admin and to use mptt for tree structure, you can use orderedmodel.mptt_models.OrderedMPTTModel
and orderedmodel.mptt_admin.OrderedMPTTModelAdmin
instead of orderedmodel.models.OrderedModel
and orderedmodel.admin.OrderedModelAdmin