编写了一个从现有插件创建 Django-CMS 插件的命令

Wrote a command that creates a Django-CMS plugin from an existing one

我有一个插件 (PodcastPlugin),其中包含两个 ManyToManyField(播客和 custom_podcasts)。我想创建一个 Django 命令,该命令在与旧实例相同的页面和占位符上创建一个新插件。 [![旧插件截图] 我可以创建一个新插件,但它不会将播客和 custom_podcasts 的旧实例复制到新创建的 PodcastPlugin 中。 这是我的代码:

from cms.models.pagemodel import Page
from cms.api import add_plugin


for page in Page.objects.all():
    for placeholder in page.placeholders.filter(page=263):
        for plugin in placeholder.get_plugins_list():
            if plugin.plugin_type == 'PodcastPlugin':
                for custom_ids in plugin.get_plugin_instance()[0].custom_podcasts.values_list('id'):
                        for podcasts_ids in plugin.get_plugin_instance()[0].podcasts.values_list('id'):
                            add_plugin(
                                placeholder=placeholder,
                                plugin_type='PodcastPlugin',
                                podcasts=[podcasts_ids[0]],
                                cmsplugin_ptr_id=plugin.id,
                                custom_podcasts=[custom_ids[0]],
                                title='New Podcast',
                                language='de'
                            )

我通过循环两个外键(播客和 custom_podcasts)的实例解决了这个问题,然后使用符号并保存它。

这是我的解决方案,以防万一有人遇到这个问题:

    for page in Page.objects.all():
        for placeholder in page.placeholders.all():
            for plugin in placeholder.get_plugins_list():
                if plugin.plugin_type == PodcastPlugin:
                    for podcasts_ids in plugin.get_plugin_instance()[0].podcasts.values_list('id'):
                        podcast_ids_list.append(podcasts_ids[0])
                    for custom_ids in plugin.get_plugin_instance()[0].custom_podcasts.values_list('id'):
                        custom_ids_list.append(custom_ids[0])

                    new_plugin = add_plugin(
                        placeholder=placeholder,
                        plugin_type=pluginname,
                        cmsplugin_ptr_id=plugin.id,
                        created_at=plugin.get_plugin_instance()[0].created_at,
                        podcasts_by_topic=plugin.get_plugin_instance()[0].podcasts_by_topic,
                        publication_date=plugin.get_plugin_instance()[0].publication_date,
                        publication_end_date=plugin.get_plugin_instance()[0].publication_end_date,
                        limit=plugin.get_plugin_instance()[0].limit,
                        collapse=plugin.get_plugin_instance()[0].collapse,
                        title=plugin.get_short_description() + ' (NEW)',
                        language='de'
                    )
                    for values in podcast_ids_list:
                        new_plugin.podcasts.add(values)
                    for values in custom_ids_list:
                        new_plugin.custom_podcasts.add(values)
                    new_plugin.save()