我和我的团队创建了一个区域,以允许我们的公司添加目标网页。我们希望在相关模型的索引视图中包括一些其他列,如该图所示。
我发现了一些旧帖子(2014年至今)表明这是不可能的,但是我找不到任何新的内容使该主张无效。可以这样做,如果可以的话,有人可以指出我正确的方向吗?
答案 0 :(得分:1)
您可以使用ModelAdmin来针对特定模型执行此操作,但是它不会像您的屏幕截图一样显示在Pages Explorer视图中。相反,它将显示在左侧栏中的新菜单项中。我还发现Hooks是存储此逻辑的好地方。请注意 ModelAdmin 与 modeladmin 有何不同。 Bakery Demo有一些很好的例子说明了这一切的原理。
答案 1 :(得分:1)
如果您愿意为页面资源管理器修补视图和模板,则应该能够执行此操作。我的小组没有为Page Explorer打补丁,因此没有相应的示例代码,但是我们的一般方法如下:
在wagtail_patches / apps.py中,我们有:
from django.apps import AppConfig
class WagtailPatchesConfig(AppConfig):
name = 'wagtail_patches'
verbose_name = 'Wagtail Patches'
ready_is_done = False
def ready(self):
"""
This function runs as soon as the app is loaded. It executes our monkey patches to various parts of Wagtail
that change it to support our architecture of fully separated tenants.
"""
# As suggested by the Django docs, we need to make absolutely certain that this code runs only once.
if not self.ready_is_done:
# The act of performing this import executes all the code in monkey_patches.
from . import monkey_patches
# Unlike monkey_patches, the code of wagtail_hook_patches is in the function patch_hooks().
from .wagtail_hook_patches import patch_hooks
patch_hooks()
self.ready_is_done = True
else:
print("{}.ready() executed more than once! This method's code is skipped on subsequent runs.".format(
self.__class__.__name__
))
然后在wagtail_patches / monkey_patches.py中,我们导入要修补的模块,然后编写一个新方法,然后用新方法替换库存版本。例如:
from wagtail.admin.forms.collections import CollectionForm
def collection_form_clean_name(self):
if <weird custom condition>:
raise ValidationError('some error message')
CollectionForm.clean_name = collection_form_clean_name