django mptt订单插入和树重建
我开始使用“django mptt订单插入和树重建,django,django-mptt,mptt,Django,Django Mptt,Mptt,我开始使用“order\u insertion\u by”来更改django mptt中的默认顺序 到目前为止,我还没有在本地服务器上发现问题,但我的测试失败了 背景: 我有一个测试夹具,它在树结构和两棵树中创建了大约10个对象 我在模型中添加了“order\u insertion\u by”。当我运行测试时,它失败了,因为树结构现在很混乱 但若我在重新加载测试夹具之后重建树,那个么树结构就可以了 这里是fixturetests.py: class TestPostCreationAndRetr
order\u insertion\u by
”来更改django mptt中的默认顺序
到目前为止,我还没有在本地服务器上发现问题,但我的测试失败了
背景:
我有一个测试夹具,它在树结构和两棵树中创建了大约10个对象
我在模型中添加了“order\u insertion\u by
”。当我运行测试时,它失败了,因为树结构现在很混乱
但若我在重新加载测试夹具之后重建树,那个么树结构就可以了
这里是fixturetests.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.
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']
在models.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.
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']
这带来了一些问题:
社区
模型是如何定义的?@arocks-Hey添加了模型。但我想我明白了。我有单独的树和对象管理器实例。在这种情况下,Community.objects.create()可能不支持树。