Python 从代码向占位符添加cms插件
我正在尝试从代码中将插件添加到占位符字段。 我有一个带有几个字段的模型(问题),其中一个是占位符字段 我想做的是向占位符字段添加一个TextPugin(或任何其他通用cms_插件)。这是必要的,因为我不希望人们从cms的前端编辑模式手动添加TextPlugin,而是自己创建它,这样他们就可以在之后添加正确的内容 我知道cms.api中有一个add_插件,但我仍然需要找到一种方法将占位符字段转换为占位符,这样它才能工作 这是我现在掌握的密码 型号.pyPython 从代码向占位符添加cms插件,python,django,plugins,placeholder,django-cms,Python,Django,Plugins,Placeholder,Django Cms,我正在尝试从代码中将插件添加到占位符字段。 我有一个带有几个字段的模型(问题),其中一个是占位符字段 我想做的是向占位符字段添加一个TextPugin(或任何其他通用cms_插件)。这是必要的,因为我不希望人们从cms的前端编辑模式手动添加TextPlugin,而是自己创建它,这样他们就可以在之后添加正确的内容 我知道cms.api中有一个add_插件,但我仍然需要找到一种方法将占位符字段转换为占位符,这样它才能工作 这是我现在掌握的密码 型号.py from django.utils.tran
from django.utils.translation import ugettext as _
from django.db import models
from djangocms_text_ckeditor.cms_plugins import TextPlugin
from cms.models.fields import PlaceholderField
from cms.api import add_plugin
class Question(models.Model):
topic = models.ForeignKey('Topic')
question = models.CharField(_("Question"),max_length=256)
answer = PlaceholderField ('Answer plugin')
priorityOrder = models.IntegerField(_("Priority Order"))
def save(self, *args, **kwargs):
# Here's the critical point: I can cast self.answer to PlaceholderField,
# but I can't cast it to a Placeholder or add a placeholder to it
add_plugin( ????, plugin_type='TextPlugin', language='us',)
super(Question, self).save(*args, **kwargs)
# set the correct name of a django.model object in the admin site
def __unicode__(self):
return self.question
class Topic(models.Model):
title = models.CharField(_("Topic title"),max_length=256)
priorityOrder = models.IntegerField(_("Priority Order"))
# set the correct name of a django.model object in the admin site
def __unicode__(self):
return self.title
欢迎提供任何帮助(包括其他方法) 一个
占位符字段
不过是一个外键
,它在创建新实例时自动创建与新的占位符
对象的关系
因此,您不能在未保存的实例上的占位符字段上使用add\u plugin
。您需要先调用super().save()
,然后调用add\u插件(self.answer,…)