diff --git a/adminsortable/utils.py b/adminsortable/utils.py index c858f88..82141d9 100644 --- a/adminsortable/utils.py +++ b/adminsortable/utils.py @@ -1,4 +1,4 @@ -from .models import Sortable +from .models import Sortable, SortableForeignKey def get_is_sortable(objects): @@ -9,9 +9,22 @@ def get_is_sortable(objects): return False +def is_self_referential(cls): + cls_type = type(cls) + sortable_subclass = issubclass(cls_type, Sortable) + sortable_foreign_key_subclass = issubclass(cls_type, SortableForeignKey) + if sortable_foreign_key_subclass and not sortable_subclass: + return True + return False + + def check_model_is_sortable(cls): if cls: - if issubclass(type(cls), Sortable): - if cls.objects.count() > 1: - return True + if is_self_referential(cls): + objects = cls.model.objects + else: + objects = cls.objects + + if objects.count() > 1: + return True return False