2017-02-11 13 views
0

Это моя первая попытка создания плагина Django CMS. Я следующие файлы готовы:Django CMS Plugin: не удается увидеть поля модели в интерфейсе

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 

Но когда я получил опцию Edit Page и попытаться добавить плагин он показывает этот экран

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 в качестве атрибута. Это позволяет создавать интерфейс администратора. Однако это не обязательно.