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: