from django.db import models
from django.core.exceptions import ValidationError


class OrderedModelManager(models.Manager):
    def swap(self, obj1, obj2):
        tmp, obj2.order = obj2.order, 0
        obj2.save(swapping=True)
        obj2.order, obj1.order = obj1.order, tmp
        obj1.save()
        obj2.save()

    def max_order(self):
        try:
            return self.order_by('-order').values_list('order', flat=True)[0]
        except IndexError:
            return 0


class OrderedModel(models.Model):
    order = models.PositiveIntegerField(blank=True, default=1, db_index=True)

    objects = OrderedModelManager()

    class Meta:
        abstract = True
        ordering = ['order']

    def save(self, swapping=False, *args, **kwargs):
        if not self.id:
            self.order = self.max_order() + 1
        if self.order == 0 and not swapping:
            raise ValidationError("Can't set 'order' to 0")
        super(OrderedModel, self).save(*args, **kwargs)

    @classmethod
    def swap(cls, obj1, obj2):
        cls.objects.swap(obj1, obj2)

    @classmethod
    def max_order(cls):
        return cls.objects.max_order()

@@ -478,16 +478,285 @@
 turn 0%0A%0A
+    def fix_ordering(self):%0A        %22%22%22%0A        This method must be executed only if this application is%0A        added to existing project.%0A        %22%22%22%0A        for index, item in enumerate(self.only('order'), 1):%0A            item.order = index%0A            item.save()%0A%0A
 %0Aclass O

Add fix_ordering method to OrderedModelManager