如何在管理面板中避免Wagtail嵌套渲染?

huangapple go评论47阅读模式
英文:

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:
如何在管理面板中避免Wagtail嵌套渲染?

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
)

huangapple
  • 本文由 发表于 2023年4月20日 05:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定