python初學-04django(資料庫,模型)__資料庫

來源:互聯網
上載者:User

一、設定資料庫

在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 pymysqlpymysql.install_as_MySQLdb()

註:如果你不需要使用INSTALLED_APPS應用中的某些app,那就在執行python manage.py migrate命令前,刪除他們就ok啦。

三、模型

開始我們的模型之旅吧

在我們的winter_app/models.py建立我們的model

from django.db import modelsclass 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

完美。

努力才能對得起自己。

上帝只給你肩膀能抗的下的成就。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.