2014-11-20 3 views
1

코드에서 PlaceholderField에 플러그인을 추가하려고합니다. 몇 가지 필드가있는 모델 (질문)이 있는데 그 중 하나는 자리 표시 자 필드입니다.코드에서 자리 표시 자에 CMS 플러그인 추가

내가하고 싶은 일은 해당 자리 표시 자 필드에 TextPugin (또는 기타 일반 cms_plugin)을 추가하는 것입니다. 사람들이 TextPlugin을 수동으로 프론트 엔드 편집 모드에서 CMS의 편집 모드로 추가하는 것을 원하지 않기 때문에 필요합니다. 오히려 올바른 컨텐츠를 추가 할 수 있도록 직접 작성하는 것입니다.

나는 cms.api에서 add_plugin을 알았지 만, 여전히 작동하려면 PlaceholderField를 PlaceholderField로 변환하는 방법을 찾아야 할 것입니다.

이것은 현재 가지고있는 코드입니다.

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 

정말 환영합니다 (이 일을 다른 방법 포함) 어떤 도움을!

답변

0

PlaceholderField은 새 인스턴스를 만들 때 Placeholder 개체에 대한 관계를 자동으로 만드는 ForeignKey입니다.

결과적으로 저장되지 않은 인스턴스의 PlaceholderField에서 add_plugin을 사용할 수 없습니다. super().save()먼저으로 전화 한 다음 add_plugin(self.answer, ...)으로 전화해야합니다.