django搭建web (四) models.py
2017-10-26 23:32
337 查看
demo
该demo模型主要是用于问题,选择单个或多个答案的问卷形式应用# -*- 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中的字段,或者注释起来
class myQuestion(admin.ModelAdmin): # fieldsets = [ # (None, {'fields': ['question_text']}), # ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), # ] inlines = [ChoiceInline] #list_display = ('question_text', 'pub_date')
这里创建了两个模型,都是继承自models
在第一个模型myQuestion中,max_length表示此处可填字段最大长度,
question_style = models.CharField(max_length = 1,choices = choice_style)
一句choices = choice_style为模型创建了一个下拉框,choice_style是前面代码定义的一个tuple如下所示:
下句是为了问题不展开时,可以以本question_text作为标题显示
def __unicode__(self): return self.question_text
以下语句是建立一对多关系(外键),一个答案可对应多个问题,具体可查---ORM
questions = models.ForeignKey(myQuestion)
以下语句设置了一个int型变量answer_votes,并初始化为0
answer_votes = models.IntegerField(default = 0)
相关文章推荐
- django搭建web (二) urls.py
- django搭建web (三) admin.py -- 待续
- 在使用django框架搭建web中如果需要单独运行py脚本出现AppRegistryNotReady("Apps aren't loaded yet.")问题
- django搭建web (五) views.py
- models.py---Django中的数据库模型
- centos7简单搭建nginx+uwsgi+web.py的Python web环境(记录)
- Django中的数据库模型类-models.py(多对多的关系)
- 基于Django的在线MOOC学习系统(5)——创建其余 App 并设计 对应的models.py
- Eclipse+PyDev+Django+Mysql搭建Python web开发环境
- Apache+MySQL+Python+Django在Windows下环境搭建指南(Python Web环境搭建)
- Python web 框架之 Django 基础搭建服务
- Django中反向生成models.py的实例讲解
- 如何从头搭建一个搜索引擎_简易的网页框架web.py
- 在Apache上搭建pywebsocket提供html5的websocket服务
- pycharm的Django搭建web开发-helloworld
- 搭建Apache+web.py+mod_wsgi python服务器
- 基于Django的在线MOOC学习系统(4)——创建 course App 并设计 models.py
- Django搭建简单网页的学习笔记 之一(models)
- 2.Django命令行工具搭建项目(django-admin.py & manage.py)