我有两个模型,其中一个是另一个的索引页:
class FooIndexPage(Page):
category = models.IntegerField(unique=True)
subpage_types = ['foo.fooPage']
class FooPage(Page):
category = models.IntegerField()
我显然漏掉了大部分字段,因为它们与我的问题无关。FooPage
的category
字段实际上是从其他字段派生的;我这么说只是为了简单
我想要实现的是:每个FooPage
最终都应该是树结构中某个FooIndexPage
的子级。但是,在创建页面之前,还不清楚哪个页面是正确的FooIndexPage
(因为category
是从其他字段派生的,如上所述)。还有一种可能性是,它还不存在,必须首先创建。当用户创建FooPage
(用户在管理界面中将页面创建为主页的子页面)时,整个过程应该会自动发生。用户保存页面后,应将其移动到相应的FooIndexPage
,可能必须首先创建该页面
我试图通过重写FooPage
的save
方法并在创建后相应地移动页面来解决此问题:
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
category = self.category
if not FooIndexPage.objects.filter(category=category).exists():
self.get_parent().add_child(instance=FooIndexPage(category=category))
index_page = FooIndexPage.objects.get(category=category)
self.move(index_page, 'last-child')
如果正确的FooIndexPage
不存在,则创建它。但是,该代码在self.move
上引发了一个异常:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/.../lib/python3.8/site-packages/wagtail/core/models.py", line 1381, in move
super().move(target, pos=pos)
File "/home/.../lib/python3.8/site-packages/treebeard/mp_tree.py", line 1095, in move
return MP_MoveHandler(self, target, pos).process()
File "/home/.../lib/python3.8/site-packages/treebeard/mp_tree.py", line 472, in process
if self.target.is_descendant_of(self.node):
AttributeError: 'NoneType' object has no attribute 'is_descendant_of'
这似乎是treebeard的一个已知问题,它发生在父页面还没有子页面时。不过,我没能找到解决办法
创建页面时是否有方法覆盖父页面?
根据the Treebeard docs:
通过创建
index_page
,可以在节点self
仍在内存中时修改树。很难知道这可能会导致什么类型的问题,但是如果例如创建索引页导致self
的path
值发生更改,那么move
操作可能会基于过时的path
值执行更新。此时可能值得运行./manage.py fixtree
,以检查树结构是否发生任何损坏在移动之前运行
self.refresh_from_db()
应该可以避免这种情况,但我并不100%相信在save
方法中这样做是安全的,因此我倾向于通过获取self
节点的新副本并在该节点上运行move
来避免这种情况:相关问题 更多 >
编程相关推荐