Django CMS:如何在设置窗口中显示数据库

时间:2017-02-13 07:11:22

标签: django 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

1 个答案:

答案 0 :(得分: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"