Skip to content

Commit

Permalink
Merge pull request #41 from ykiu/support-abstract-model
Browse files Browse the repository at this point in the history
Support subclassing ClosureTree to make abstract model
  • Loading branch information
mikebryant authored Nov 6, 2017
2 parents ce874a2 + cd625d6 commit d6e4255
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion closuretree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(cls, name, bases, dct):
to doing all the normal django stuff.
"""
super(ClosureModelBase, cls).__init__(name, bases, dct)
if not cls._meta.get_parent_list() and cls.__module__ != __name__:
if not cls._meta.get_parent_list() and not cls._meta.abstract:
setattr(
sys.modules[cls.__module__],
'%sClosure' % cls.__name__,
Expand Down
30 changes: 30 additions & 0 deletions closuretree/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,33 @@ def test_basic(self):
c = TCNoMeta.objects.create(name='c', parent=b)
self.failUnlessEqual(a.get_descendants().count(), 2)
self.failUnlessEqual(c.get_ancestors().count(), 2)

class TCAbstract(ClosureModel):
"""A test model with Meta.abstract = True."""

class Meta:
abstract = True

parent = models.ForeignKey(
"self",
related_name="children",
null=True,
blank=True
)
name = models.CharField(max_length=32)

class TCConcrete(TCAbstract):
"""A test model inheriting from abstract base model."""
pass

class AbstractModelTestCase(TestCase):
"""Test a concrete model inherited from an abstract model."""

normal_model = TCConcrete
closure_model = TCConcreteClosure

def test_closure_table_of_concrete_model(self):
"""Test the closure table of the concrete model operates correctly."""
self.a = self.normal_model.objects.create(name="a")
self.b = self.normal_model.objects.create(name="b", parent=self.a)
self.assertEqual(self.closure_model.objects.count(), 3)

0 comments on commit d6e4255

Please sign in to comment.