0

I started using 'order_insertion_by' to change the default ordering in django-mptt.

So far I haven't found issues on my local, but my tests fail.

Background: I have test fixture which creates about 10 objects in tree structure and two trees.

I added 'order_insertion_by' in models. And when I run tests, it fails because the tree structure is in mess now.

But if I do tree rebuild, just after reloading test fixtures, tree structure is fine.

Here is fixture tests.py:

class TestPostCreationAndRetrival(LiveServerTestCase):

    headers = {'Accept': 'application/json; indent=4'}

    def fixture(self):
        self.school = Community.objects.create(name='TestSchool')
        self.school2 = Community.objects.create(name='NewTestschool')
        self.classroom1 = Community.objects.create(parent=self.school,
            name = 'Classroom1')
        self.classroom2 = Community.objects.create(parent=self.school,
            name = 'Classroom2')
        self.subclassroom1 = Community.objects.create(
            parent=self.classroom1,
            name='Subclassroom1')
        self.subclassroom2 = Community.objects.create(
            parent=self.classroom2,
            name='Subclassroom2')
        self.subclassroom3 = Community.objects.create(
            parent=self.classroom1,
            name='Subclassroom3')
        self.subclassroom4 = Community.objects.create(
            parent=self.classroom2,
            name='Subclassroom4')

        self.gou1 = Community.objects.create(
            parent=self.subclassroom1,
            name='GOC1')
        self.gou2 = Community.objects.create(
            parent=self.subclassroom2,
            name='GOC2')

def setUp(self):
    self.fixture()
    # This prints empty qs [] while I gave children in above fixture.
    print Community.objects.get(name='TestSchool').get_descendants()
    # Let's rebuild the tree
    Community.tree.rebuild()
    print Community.objects.get(name='TestSchool').get_descendants()
    # prints all the descendants just fine.

And in models.py:

class Community(MPTTModel):
    tree = TreeManager()
    parent = models.ForeignKey('self', verbose_name=_(u'Parent'),
        null=True, blank=True, related_name='children')

    name = models.CharField(verbose_name=_(u'Name'),
        max_length=100)
    slug = models.SlugField(verbose_name=_(u'Slug'),
        max_length=100)

    objects = CommunityManager()

   class MPTTMeta:
       order_insertion_by = ['name']

This raises few issues:

  1. I think 'order_insertion_by' forces to update/rebuild tree when an object is created/updated. But this is not consistent and you have to rebuild after model creation/update yourself just to make sure tree is in good state. Is it so?

  2. In my test cases, why is the initial tree in mess? I only created about 10 objects with tree structure.

chhantyal
  • 11,874
  • 7
  • 51
  • 77
  • How is the `Community` model defined? – arocks Jan 23 '14 at 08:38
  • @arocks Hey added models. But I think I got the issue. I have separate tree and objects manager instances. In that case, Community.objects.create() might not be tree aware. – chhantyal Jan 23 '14 at 08:45

0 Answers0