From 612c4f80313363a0aff787310a0d31b37c9d6fc6 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Mon, 3 Nov 2014 10:43:41 -0500 Subject: [PATCH] Added method to check for self-referential sortable foreign keys. --- adminsortable/utils.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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