简体   繁体   中英

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

I have a plugin (PodcastPlugin) that contains two ManyToManyField (podcasts and custom_podcasts). I want to create a Django command that creates a new plugin on the same page and placeholder with the old instances. 老插件 [![Old Pluguin screenshot] I can create a new plugin but it does not copy the old instances of podcasts and custom_podcasts into the newly created PodcastPlugin. 创建的插件 Here is my code:

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'
                            )

I solved the problem by looping through the instances of both foreign keys (podcasts and custom_podcasts) and then using notation and to save it.

Here is my solution in case anyone come across this:

    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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM