Django是符合MVC架构的,这里现学习M—Model,而且Django自带了一个管理model(数据库)的界面,所以一并学习。

Database 配置

编辑Django的配置文件settings.py进行配置

添加polls app,修改后如下

INSTALLED_APPS = [
    'django.contrib.admin',      # 管理界面
    'django.contrib.auth',       # 认证系统
    'django.contrib.contenttypes',  # 框架的content type
    'django.contrib.sessions',     # session framework
    'django.contrib.messages',     # messages framework
    'django.contrib.staticfiles',   # 管理静态文件的framework
    'polls.apps.PollsConfig',      # 我们自己的app
]

最后一行位新添加的,表示新增一个app,类polls.apps.PoolsConfig定义了app,名称为“polls”(可以打开这个类看到)。

还可以看到很多其他的app,我们之前说过,一个project可以有多个app,一个app可以属于多个project,这个的实现方式就是这样,每个app都位于不同的包下面,如果一个project想包含一个app,只需要把这个app的包的配置写在这儿就可以了。

接下来配置数据库,修改完之后如下

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'polls.db',
    }
}

Django支持大多数主流的数据库,包括postgresql,mysql,sqlite等,这里为了简单就直接用sqlite,如果使用mysql应该配置成如下(其他类似)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

数据库至此配置完成,接下来就是创建model

创建model

编辑mysite/polls/models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Question(models.Model):
   # CharField:字段是字符串类型,有一个必要参数——字符串长度 question_text
= models.CharField(max_length=200)
   # DateField:字段是日期类型 publ_date
= models.DateField('date published')

    def __unicode__(self):
        return self.question_text
class Choice(models.Model):
   # question作为choice的外键,这里是多对一关系,删除的时候进行级联,Django还支持:many-to-one, many-to-many, and one-to-one. question
= models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.choice_text

每个model继承自models.Model,会继承一些常用的操作方法。每个class对应的属性都对应数据库中表中的字段——这也就是ORM

生成数据库表结构

# 告诉Django model改变了,并且保存为一个migration
python manage.py makemigrations

运行该命令,输出如下内容,并在pools/migrations下生成0001_initial.py

Migrations for 'polls':
  0001_initial.py:
    - Create model Choice
    - Create model Question
    - Add field question to choice

我们可以看看Django根据这个migration会怎么生成表结构运行

# 0001 为之前生成的0001_initial.py的前缀,表示第一个版本的migration
python manage.py sqlmigrate polls 0001

输出

BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_test" varchar(200) NOT NULL, "publ_date" date NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "polls_choice" RENAME TO "polls_choice__old";
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
INSERT INTO "polls_choice" ("choice_text", "votes", "id", "question_id") SELECT "choice_text", "votes", "id", NULL FROM "polls_choice__old";
DROP TABLE "polls_choice__old";
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");

COMMIT;
View Code

相关文章:

  • 2022-01-04
  • 2022-12-23
  • 2021-09-17
  • 2021-05-18
  • 2021-11-06
  • 2021-07-05
  • 2022-01-02
猜你喜欢
  • 2021-08-17
  • 2021-07-13
  • 2021-08-10
  • 2021-04-28
  • 2022-01-20
  • 2021-12-15
相关资源
相似解决方案