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" 

Я хочу показать опрос в Edit Setting Window.

Как заполнить surveys от значения db?

+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"