python初学-04django(数据库,模型)

一、配置数据库

在winter下的settings.py文件,默认是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
如果需要配置其他数据库:

ENGINE值及对应库

'django.db.backends.sqlite3':sqlite3数据库,

'django.db.backends.postgresql':postgresql数据库,

'django.db.backends.mysql':mysql数据库,

'django.db.backends.oracle'. oracle数据库

其他值可参考

使用其他库的参数

举例:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
二、installed_apps简述

设置在文件的顶部的installed_apps。管理着当前项目应用的所有django应用程序的名字。应用程序可以在多个项目中使用,你可以打包和分发它们供其他人在他们的项目中使用。

INSTALLED_APPS参数的含义:

django.contrib.admin – 管理站点app
django.contrib.auth – 管理权限系统的app
django.contrib.contenttypes – 内容类型的app
django.contrib.sessions – 管理session的app
django.contrib.messages – 管理消息的app
django.contrib.staticfiles – 管理静态文件的app

这些应用程序中一些需要使用数据库表,所以使用这些程序,需要我们创建表,命令:

 python manage.py migrate(执行后表创建在上面配置的数据库中)

谨记:在winter/_init_.py里面添加

import pymysql
pymysql.install_as_MySQLdb()

注:如果你不需要使用INSTALLED_APPS应用中的某些app,那就在执行python manage.py migrate命令前,删除他们就ok啦。

三、模型

开始我们的模型之旅吧

在我们的winter_app/models.py创建我们的model

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)
激活models(对应的库建表)

1)、在winter_app/settings.py下面添加

INSTALLED_APPS = [
    'winter_app.apps.WinterAppConfig',#自己winter_app下面apps.py里面
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
2)python manage.py makemigrations winter_app  (通知项目你新添加了app)

你会看到winter_app/migrations/0001_initial.py,变动的信息

3)python manage.py sqlmigrate winter_app 0001 

BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" (
    "id" serial NOT NULL PRIMARY KEY,
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL
);
--
-- Create model Question
--
CREATE TABLE "polls_question" (
    "id" serial NOT NULL PRIMARY KEY,
    "question_text" varchar(200) NOT NULL,
    "pub_date" timestamp with time zone NOT NULL
);
--
-- Add field question to choice
--
ALTER TABLE "polls_choice" ADD COLUMN "question_id" integer NOT NULL;
ALTER TABLE "polls_choice" ALTER COLUMN "question_id" DROP DEFAULT;
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");
ALTER TABLE "polls_choice"
  ADD CONSTRAINT "polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id"
    FOREIGN KEY ("question_id")
    REFERENCES "polls_question" ("id")
    DEFERRABLE INITIALLY DEFERRED;

COMMIT;
4)python manage.py migrate

完美!

努力才能对得起自己!

上帝只给你肩膀能抗的下的成就!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值