Merged pull request from kynazz to add support for unicode in sort view title.
Updated readme and version.
This commit is contained in:
+20
-13
@@ -14,22 +14,26 @@ class MultipleSortableForeignKeyException(Exception):
|
||||
|
||||
class Sortable(models.Model):
|
||||
"""
|
||||
Unfortunately, Django doesn't support using more than one AutoField in a model
|
||||
or this class could be simplified.
|
||||
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.
|
||||
`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
|
||||
`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
|
||||
`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.
|
||||
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)
|
||||
order = models.PositiveIntegerField(editable=False, default=1,
|
||||
db_index=True)
|
||||
|
||||
# legacy support
|
||||
sortable_by = None
|
||||
@@ -41,7 +45,8 @@ class Sortable(models.Model):
|
||||
@classmethod
|
||||
def is_sortable(cls):
|
||||
try:
|
||||
max_order = cls.objects.aggregate(models.Max('order'))['order__max']
|
||||
max_order = cls.objects.aggregate(
|
||||
models.Max('order'))['order__max']
|
||||
except (TypeError, IndexError):
|
||||
max_order = 0
|
||||
return True if max_order > 1 else False
|
||||
@@ -59,12 +64,14 @@ class Sortable(models.Model):
|
||||
if isinstance(field, SortableForeignKey):
|
||||
sortable_foreign_keys.append(field)
|
||||
if len(sortable_foreign_keys) > 1:
|
||||
raise MultipleSortableForeignKeyException(u'%s may only have one SortableForeignKey' % self)
|
||||
raise MultipleSortableForeignKeyException(
|
||||
u'%s may only have one SortableForeignKey' % self)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.id:
|
||||
try:
|
||||
self.order = self.__class__.objects.aggregate(models.Max('order'))['order__max'] + 1
|
||||
self.order = self.__class__.objects.aggregate(
|
||||
models.Max('order'))['order__max'] + 1
|
||||
except (TypeError, IndexError):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user