django-admin-sortable/adminsortable/models.py
Brandon Taylor b16def5c09 Merged changes from sortable-by-refactor.
Incremented version to 1.2.
Refactored ORM calls to properly order objects by the sortable_by property to ensure objects are grouped correctly in the sortable change list template after being passed through dynamic_regroup.
Fixed missing import for jquery.effects.core, again.
Refactored sortable_by classmethod into a property.
2011-11-22 22:27:20 -06:00

47 lines
1.6 KiB
Python
Executable file

from django.contrib.contenttypes.models import ContentType
from django.db import models
class Sortable(models.Model):
"""
Unfortunately, Django doesn't support using more than one AutoField in a model
or this class could be simplified.
`is_sortable` determines whether or not the Model is sortable by determining
if the last value of `order` is greater than the default of 1, which should be
present if there is only one object.
`model_type_id` returns the ContentType.id for the Model that inherits Sortable
`save` the override of save increments the last/highest value of order by 1
Override `sortable_by` method to make your model be sortable by a foreign key field.
Set `sortable_by` to the class specified in the foreign key relationship.
"""
order = models.PositiveIntegerField(editable=False, default=1, db_index=True)
sortable_by = None
class Meta:
abstract = True
ordering = ['order']
@classmethod
def is_sortable(cls):
try:
max_order = cls.objects.aggregate(models.Max('order'))['order__max']
except TypeError, IndexError:
max_order = 0
return True if max_order > 1 else False
@classmethod
def model_type_id(cls):
return ContentType.objects.get_for_model(cls).id
def save(self, *args, **kwargs):
if not self.id:
try:
self.order = self.__class__.objects.aggregate(models.Max('order'))['order__max'] + 1
except TypeError, IndexError:
pass
super(Sortable, self).save(*args, **kwargs)