2017-02-11 31 views
0

这是我第一次尝试制作Django CMS插件。我有以下文件准备就绪:Django CMS插件:无法在界面中看到模型字段

cms_plugins.py

from cms.plugin_base import CMSPluginBase 
from cms.plugin_pool import plugin_pool 
from cms.models import CMSPlugin 

from . import models 


class SurveyPluginPublisher(CMSPluginBase): 
    """Show Polls entered by Admin.""" 

    cache = False 
    # model = models.QuickAidPluginModel 
    module = "Survey" 
    name = "Awesome Survey v1.0" 
    render_template = 'survey/_hello.html' 

    def render(self, context, instance, placeholder): 
     return context 


plugin_pool.register_plugin(SurveyPluginPublisher) 

models.py

# encoding: utf-8 
from cms.models import CMSPlugin, python_2_unicode_compatible 
from django.db import models 
from django.core.exceptions import ValidationError 
from cms.models import CMSPlugin 


class Survey(models.Model): 
    name = models.CharField(max_length=400) 
    description = models.TextField() 

    def __unicode__(self): 
     return (self.name) 

    def questions(self): 
     if self.pk: 
      return Question.objects.filter(survey=self.pk) 
     else: 
      return None 

@python_2_unicode_compatible 
class SurveyPluginModel(CMSPlugin): 
    name = models.CharField("Survey Name", max_length=255, default='Survey Name', 
          help_text='Enter Survey Name') 
    description = models.CharField("Survey Description", max_length=500, blank=True, help_text='Write Description here') 

    def __str__(self): 
     return "Returning some Survey Text" 

模板文件

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
    <h2>Hi Survey</h2> 
</body> 
</html 

但是,当我编辑页面的选项,并尝试加入插件,它显示了这个屏幕

enter image description here

+2

我想这是因为你已经在你的插件类中定义了'module'而不是'model',因此它不知道你有什么字段。这是一个插件类的例子; https://github.com/divio/djangocms-link/blob/master/djangocms_link/cms_plugins.py –

回答

0

尝试声明形式:

forms.py:

class SurveyForm(forms.ModelForm): 
    class Meta: 
     model = Survey 
     field = ['name', 'description'] 

models.py:

# encoding: utf-8 
from cms.models import CMSPlugin, python_2_unicode_compatible 
from django.db import models 
from django.core.exceptions import ValidationError 
from cms.models import CMSPlugin 


class Survey(models.Model): 
    name = models.CharField(max_length=400) 
    description = models.TextField() 

    def __unicode__(self): 
     return (self.name) 

    def questions(self): 
     if self.pk: 
      return Question.objects.filter(survey=self.pk) 
     else: 
      return None 

@python_2_unicode_compatible 
class SurveyPluginModel(CMSPlugin): 
    name = models.CharField("Survey Name", max_length=255, default='Survey Name', 
         help_text='Enter Survey Name') 
    description = models.CharField("Survey Description", max_length=500, blank=True, help_text='Write Description here') 
    form = SurveyForm 

    def __str__(self): 
     return "Returning some Survey Text" 
0

尝试将一行model = models.SurveyPluginModel添加到您的SurveyPluginPublisher。它需要知道它的模型。

此外,我建议添加fieldsets作为属性。它允许设计管理界面。然而,这不是必需的。