自定义插件上的默认子插件

2024-09-28 22:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为MultiColumnResponsive的定制插件,它接收列数作为参数,并且只接受子级的ColumnResponsive插件。我想创建默认情况下作为子级嵌套的ColumnResponsive插件,但我无法做到这一点。你知道吗

我现在的代码是:

厘米_插件.py

class MultiColumnResponsivePlugin(CMSPluginBase):
    name = "Multi Column Responsive"
    module = _("Containers")
    model = MultiColumn
    render_template = "plugin/multi-column-responsive/multi-column-responsive.html"
    allow_children = True
    child_classes = ["ColumnResponsivePlugin"]

    def render(self, context, instance, placeholder):
        context = super(MultiColumnResponsivePlugin, self).render(context, instance, placeholder)
        return context

class ColumnResponsivePlugin(CMSPluginBase):
    name = "Column Responsive"
    module = _("Containers")
    render_template = "plugin/column-responsive/column-responsive.html"
    allow_children = True
    parent_classes = ["MultiColumnResponsivePlugin"]

    def render(self, context, instance, placeholder):
        context = super(ColumnResponsivePlugin, self).render(context, instance, placeholder)
        return context

型号.py

class MultiColumn(CMSPlugin):
    NUM_OF_COLUMNS = (
        (1, '1'),
        (2, '2'),
    )
    num_of_columns = models.IntegerField(default=1, choices=NUM_OF_COLUMNS)

这是我添加一个包含两列的多列响应插件时所期望的结果:

enter image description here

编辑:经过几天的研究。我通过在MultiColumnResponsivePlugin类的save_model函数中添加几行代码实现了这一点:

def save_model(self, request, obj, form, change):
    response = super(MultiColumnResponsivePlugin, self).save_model(
        request, obj, form, change
    )
    for x in range(int(form.cleaned_data['num_of_columns'])):
        add_plugin(obj.placeholder, ColumnResponsivePlugin.__name__, obj.language, target=obj)
    return response

Tags: instancenameself插件objmodelcontextcolumn
1条回答
网友
1楼 · 发布于 2024-09-28 22:22:23

你可以试试这个:

from django.forms import ModelForm, NumberInput
from cms.api import add_plugin

class MultiColumnResponsiveForm(ModelForm):
    wanted_children = NumberInput()

    class Meta:
        model = MultiColumn
        fields = '__all__'

class MultiColumnResponsivePlugin(CMSPluginBase):
    name = "Multi Column Responsive"
    module = _("Containers")
    model = MultiColumn
    render_template = "plugin/multi-column-responsive/multi-column-responsive.html"
    allow_children = True
    child_classes = ["ColumnResponsivePlugin"]
    form = MultiColumnResponsiveForm

    def save_model(self, request, obj, form, change):
        wanted_children = form.cleaned_data['wanted_children']
        super().save_model(request, obj, form, change)
        self.extend_children(obj, wanted_children)

    def extend_children(self, parent, wanted_children):
        current_children = parent.get_num_children()
        for _ in range(current_children, wanted_children):
            child = add_plugin(parent.placeholder, ColumnResponsivePlugin,
                               parent.language, target=parent)
            child.save()

关于这一点的一些注意事项:

如果您需要一个临时值,比如wanted_children,请使用一个表单,不要污染您的模型!你知道吗

此代码段采用了djangocms-cascade,它使用类似的方法来实现相同的结果。你知道吗

相关问题 更多 >