該demo模型主要是用於問題,選擇單個或多個答案的問卷形式應用html
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models # Create your models here. choice_style = ( ('r','radio'), ('c','checkbox') ) class myQuestion(models.Model): question_text = models.CharField(max_length = 600) question_style = models.CharField(max_length = 1,choices = choice_style) def __unicode__(self): return self.question_text class myAnswer(models.Model): answer_text = models.CharField(max_length = 200) questions = models.ForeignKey(myQuestion) answer_votes = models.IntegerField(default = 0) def __unicode__(self): return self.answer_text
同時!要修改admin.py中的字段,或者註釋起來python
class myQuestion(admin.ModelAdmin): # fieldsets = [ # (None, {'fields': ['question_text']}), # ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), # ] inlines = [ChoiceInline] #list_display = ('question_text', 'pub_date')
這裏建立了兩個模型,都是繼承自modelsdjango
question_style = models.CharField(max_length = 1,choices = choice_style)
def __unicode__(self): return self.question_text
questions = models.ForeignKey(myQuestion)
answer_votes = models.IntegerField(default = 0)