gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:57 25 4
gpt4 key购买 nike

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

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

关于python - 编写了一个从现有插件创建 Django-CMS 插件的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com