Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从代码向占位符添加cms插件_Python_Django_Plugins_Placeholder_Django Cms - Fatal编程技术网

Python 从代码向占位符添加cms插件

Python 从代码向占位符添加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

我正在尝试从代码中将插件添加到占位符字段。 我有一个带有几个字段的模型(问题),其中一个是占位符字段

我想做的是向占位符字段添加一个TextPugin(或任何其他通用cms_插件)。这是必要的,因为我不希望人们从cms的前端编辑模式手动添加TextPlugin,而是自己创建它,这样他们就可以在之后添加正确的内容

我知道cms.api中有一个add_插件,但我仍然需要找到一种方法将占位符字段转换为占位符,这样它才能工作

这是我现在掌握的密码

型号.py

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,…)