英文:
How to avoid wagtail nested rendering in the admin panel?
问题
class Field1Stream(blocks.StreamBlock):
button1 = AnotherBlock(label="主按钮")
button2 = AnotherBlock(label="辅助按钮")
class Meta:
icon = "链接"
required = False
block_counts = {
"button1": {"max_num": 1},
"button2": {"max_num": 1},
}
template = "my_template.html"
class NavBar(ClusterableModel, PreviewableMixin):
...
button = StreamField(
[("field1", Field1Stream()), ],
use_json_field=True, max_num=1
)
在NavBar模型内使用Field1Stream时,它会创建一个嵌套结构。如果我有很多Field1Stream()那么没问题,但在这里我只有一个Field1Stream(),这就是为什么我不想在这里创建嵌套结构。通过嵌套我指的是如何避免它?
英文:
let's say I have two models like below:
class Field1Stream(blocks.StreamBlock):
button1 = AnotherBlock(label="button main")
button2 = AnotherBlock(label="button secondary")
class Meta:
icon = "link"
required = False
block_counts = {
"button1": {"max_num": 1},
"button2": {"max_num": 1},
}
template = "my_template.html"
class NavBar(ClusterableModel, PreviewableMixin):
...
button = StreamField(
[("field1", Field1Stream()), ],
use_json_field=True, max_num=1
)
When I use Field1Stream
inside NavBar
model, it will create a nested structure. If I have a lot of Field1Stream()
then it is okay but here I have only one Field1Stream()
that's why I don't want to create a nested structure here.
With nested I mean like:
How can I avoid it?
答案1
得分: 0
当您将列表传递给StreamField
,例如[("field1", Field1Stream()), ]
,它会隐式将其转换为StreamBlock,用作流的顶级。在这种情况下,这将导致StreamBlock嵌套在StreamBlock中。要避免这种情况,请直接将Field1Stream()
作为StreamField
的参数传递:
button = StreamField(
Field1Stream(),
use_json_field=True, max_num=1
)
英文:
When you pass a list to StreamField
, such as [("field1", Field1Stream()), ]
, it will implicitly turn that into a StreamBlock to use as the top-level of the stream. In this case, this results in a StreamBlock embedded in a StreamBlock. To avoid that, pass Field1Stream()
directly as the argument to StreamField
:
button = StreamField(
Field1Stream(),
use_json_field=True, max_num=1
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论