【问题标题】:Adding a cms plugin to a placeholder from code从代码中将 cms 插件添加到占位符
【发布时间】:2015-01-18 15:38:09
【问题描述】:

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

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

我知道 cms.api 中有 add_plugin,但我仍然需要想办法将 PlaceholderField 转换为 Placeholder 才能正常工作。

这是我现在的代码。

models.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

非常欢迎任何帮助(包括其他方法)!

【问题讨论】:

    标签: python django plugins placeholder django-cms


    【解决方案1】:

    PlaceholderField 只不过是 ForeignKey,它会在创建新实例时自动创建与新 Placeholder 对象的关系。

    因此,您不能在未保存的实例上的PlaceholderField 上使用add_plugin。您需要先致电super().save(),然后再致电add_plugin(self.answer, ...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 2012-10-10
      • 2016-05-04
      • 2015-09-09
      • 2015-10-21
      • 1970-01-01
      • 2018-05-05
      相关资源
      最近更新 更多