Django數(shù)據(jù)模型及管理后臺(tái)
啟動(dòng)一個(gè)Django項(xiàng)目
安裝:
$ sudo -H pip install django==1.8.5
創(chuàng)建項(xiàng)目并運(yùn)行:
$ django-admin startproject [project]
$ mv [project]/* [git folder]/
$ rm -rf [project]
$ cd [project]
加載初始數(shù)據(jù) 生成sqlite3數(shù)據(jù)庫
$ python manage.py migrate
運(yùn)行服務(wù)器
$ python manage.py runserver
學(xué)會(huì)多留意終端輸出的信息
論壇的基本組成部分
- 用戶
- 板塊
- 文章
- 評(píng)論
- 消息
創(chuàng)建一個(gè)小應(yīng)用(APP)
創(chuàng)建目錄結(jié)構(gòu):
$ python manage.py startapp [app]
在全局配置中 注冊(cè)應(yīng)用:
在 [project]/setting.py 中的 INSTALLED_APPS 添加 [app]
-
注意:如果未注冊(cè)應(yīng)用就運(yùn)行服務(wù)會(huì)報(bào)錯(cuò):
no such table xxx_xxx
- 注冊(cè)應(yīng)用后同步數(shù)據(jù)庫即可解決
- 未注冊(cè)的話,在同步時(shí)數(shù)據(jù)庫不用找到更新的內(nèi)容。
定義數(shù)據(jù)模型
file: block/models.py
from django.contrib.auth.models import User # django默認(rèn)用戶體系
from django.db import models
class Demo(models.Modle): # django的所有數(shù)據(jù)模型都要繼承這個(gè)類
# CharField 字符串列
example4char = models.CharField(max_lenth=30)
example4int = models.IntegerField()
# IntegerField 整數(shù)列 choices限制整數(shù)為1,2,后面是對(duì)1,2的描述
sex = models.IntegerField(chioces=((1, u"男"), (2, u"女")))
# ForeignKey 外鍵 從外部數(shù)據(jù)庫引入一個(gè)數(shù)據(jù)表
# owner的可能值是User里面的行
# owner 必須是User一個(gè)用戶
# owner_id 就是User的id的字段所有取值的可能
owner = models.ForeignKey(User, verbose_name="作者")
# 幾乎所有的數(shù)據(jù)庫表都應(yīng)該都有這兩個(gè)字段
create_timestamp = models.DateTimeField(auto_now_add=True)
last_update_timestamp = models.DateTimeField(auto_add=True)
首期目標(biāo)數(shù)據(jù)模型
板塊(Block):
名稱
描述
管理員創(chuàng)建時(shí)間戳
更新時(shí)間戳
同步 數(shù)據(jù)模型 到 數(shù)據(jù)庫
$ python manage.py makemigrations
$ python manage.py migrate
- makemigrations 是django對(duì)數(shù)據(jù)庫的改動(dòng)做記錄
- migrate 對(duì)數(shù)據(jù)庫進(jìn)行實(shí)際的修改
閱讀文檔
- 地址
The model layer --> models --> Field types - 兩個(gè)部分
Field的屬性 和 Field的類型
強(qiáng)大的Admin
自動(dòng)生成管理后臺(tái)
操作
在總urls.py加入
admin.autodiscover()
在命令中創(chuàng)建一個(gè)超級(jí)管理員
$ python manage.py createsuperuser
Admin 管理我們的數(shù)據(jù)表
在 block/admin.py 中添加
from django.contrib import admin
from models import Block
admin.site.register(Block)
美化漢化我們的數(shù)據(jù)表
在 block/admin.py 中
from django.contrib import admin
from models import Block
class BlockAdmin(admin.ModelAdmin):
list_display = ("a", "a", "c", "d")
list_filter = ("e",) # 過濾器
search_field = ("f") # 搜索欄 不能搜索外鍵
admin.site.register(Block, BlockAdmin) # 在注冊(cè)時(shí)放在后面
漢化我們的數(shù)據(jù)表
from django.contrib.auth.models import User
from django.db import models
class Demo(models.Modle):
example4char = models.CharField(u"字符范例", max_lenth=30)
example4int = models.IntegerField(u"數(shù)字范例")
sex = models.IntegerField(u"性別", chioces=((1, u"男"), (2, u"女")))
owner = models.ForeignKey(User, verbose_name="作者")
create_timestamp = models.DateTimeField(auto_now_add=True)
last_update_timestamp = models.DateTimeField(auto_add=True)
def __unicode__(self):
return self.example4char
class Meta: # 元數(shù)據(jù)
verbose_name = u"模板"
verbose_name_plural = u"模板" # 復(fù)數(shù)
- 在 block/models.py 中
- 加上第一個(gè)參數(shù)說明,處理外鍵需要關(guān)鍵字參數(shù)verbose_name
- unicode 定義這個(gè)數(shù)據(jù)模型的名字說明
- Meta class 指的是整個(gè)表的特性或特征