一半教程用的Django都是1.8之前的版本,致使跟我用的1.8.2的版本用法有些出入,因此只能本身去官網看文檔,如下一下是看官方文檔而理解的,英語渣渣,可能會有理解有誤的地方python
先記錄下如何查看django的版本:數據庫
>>>python >>>import django >>>django.VERSION (1, 8, 2, 'final', 0)
把官網的那段示例Model copy過來,爲了加深印象,我本身會手寫一邊:django
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): # __unicode__ on Python 2 return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): # __unicode__ on Python 2 return self.name class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() n_pingbacks = models.IntegerField() rating = models.IntegerField() def __str__(self): # __unicode__ on Python 2 return self.headline
抄完結果結構後在dos虛擬環境下咱們來驗證下有沒有錯誤:session
>>>python manage.py check
System check identified no issues ( 0 silenced).
表示沒錯,接下來生成數據庫遷移文件(未同步到數據庫):app
>>>python manage.py makemigrations --name frist Migrations for 'myLesson' 001__frist.py: - Create model Author - Create model Blog - Create model Entry
--name 後面爲這次遷移標識的名字,最終生成0001_frist.py,該文件在app目錄下/migrationside
最後咱們來同步下更改spa
>>>python manage.py migrate
....省略
Applying sessions.001_initital... OK
我用的默認配置的Sqlite3數據庫,使用Navicat打開以下:code
紅框內是Django爲咱們定義的實體生成的表blog
---吃飯先教程