码迷,mamicode.com
首页 > 其他好文 > 详细

django系列3 :创建模型

时间:2018-09-26 12:18:53      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:编辑   概念   port   关系   models   text   ade   lis   bsp   

1创建模型

在我们简单的民意调查应用程序中,我们将创建两个模型:QuestionChoiceQuestion有问题和出版日期。Choice有两个字段:选择的文本和投票记录。每个Choice都与一个Question

这些概念由简单的Python类表示。编辑 polls/models.py文件,使其如下所示:

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(‘date published‘)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

 这里的choice和question是多对一的关系,所以choice里面有一个ForeignKey,关联了question.

 这里的CASCADE,是如果question表中的记录被删除,则choice表中对应的记录自动被删除技术分享图片

 

django系列3 :创建模型

标签:编辑   概念   port   关系   models   text   ade   lis   bsp   

原文地址:https://www.cnblogs.com/zhizhiyin/p/9706104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!