Django Admin Background Operations database take a survey as an example

Source: Internet
Author: User

Django's backstage function is very powerful, I first write a little bit (I novice).

1, first create the database (I use the python2.7):

The characters displayed in the field are for easy background management

 class Thequestion (models. Model): 
Name=models. Charfield (U ' questionnaire name ', max_length=30)
Number=models. Integerfield (U ' questionnaire filled in number ', default=0)
Create_time=models. Datetimefield (auto_now_add=true)

def __unicode__ (self):
return self.name

class questionproble M (models. Model):
Questions=models. ForeignKey (thequestion)
Problem_name=models. Charfield (the name of the U ' title ', max_length=200)
Ques_type=models. Booleanfield (Type of U ' problem ', default=false) #为False时为单选题


def __unicode__ (self):
return self.problem_name< br>
Class Choice (models. Model):
Question=models. ForeignKey (Questionproblem)
Choice_text=models. Charfield (U ' option name ', max_length=200)
Votes=models. Integerfield (Number of U ' selections ', default=0)

def __unicode__ (self):
return self.choice_text

2, operation in admin.py, temporarily do not know how to integrate the questionnaire name + title + options on the same page
Class Questioninline (admin. Tabularinline): #为了在问卷页面输入你的问卷题目这是默认有6题
Model = Questionproblem
Extra = 6


Class Choiceinline (admin. Tabularinline): 4 options per question
Model = Choice
Extra = 4


Class Questiionadmin (admin. Modeladmin):
FieldSets = [
(None, {' Fields ': [' Problem_name ']}), #在后台可以看到问题名称
(None, {' Fields ': [' Ques_type ']}), #题的类型
]
Inlines = [Choiceinline] #题对应的选项


Class Theadmin (admin. Modeladmin):
FieldSets = [
(None, {' Fields ': [' name ']}), # The name of the questionnaire
(None, {' Fields ': [' number ']}), #问卷答题数量

]
Inlines = [Questioninline] #问卷对应的题


Admin.site.register (Questionproblem, Questiionadmin)
Admin.site.register (Thequestion, Theadmin)
Admin.site.register (Choice)




Django Admin Background Operations database take a survey as an example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.