2017-02-13 12 views
0

我想在设置窗口中从数据库的下拉菜单中显示数据。现在我从硬编码数组显示。Django CMS:如何从设置窗口中的数据库中显示

MY_CHOICES = (
     ('a', 'Cat1'), 
     ('b', 'Cat2'), 
    ) 
    categories = models.CharField("Survey", help_text="Select Survey", choices=MY_CHOICES, max_length=3, blank=True) 

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 

class SurveyPluginModel(CMSPlugin): 
    MY_CHOICES = (
     ('a', 'Cat1'), 
     ('b', 'Cat2'), 
    ) 
    categories = models.CharField("Survey", help_text="Select Survey", choices=MY_CHOICES, max_length=3, blank=True) 

    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" 

我想告诉调查编辑设置窗口。

如何从db值填入surveys

+0

可能是用户ForeignKey的领域或ManyToManyField? – Amar

+0

你能详细说明一下吗? – Volatil3

+0

我假设你想将你的选择保存到数据库而不是定义的元组中? – Amar

回答

1

试试这个

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 

class SurveyPluginModel(CMSPlugin): 
    categories = models.ForeignKey("Survey", help_text="Select Survey", max_length=3, blank=True) 

    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" 
相关问题