Python学习笔记:Day4 编写Model
前言
最近在學(xué)習(xí)深度學(xué)習(xí),已經(jīng)跑出了幾個(gè)模型,但Pyhton的基礎(chǔ)不夠扎實(shí),因此,開(kāi)始補(bǔ)習(xí)Python了,大家都推薦廖雪峰的課程,因此,開(kāi)始了學(xué)習(xí),但光學(xué)有沒(méi)有用,還要和大家討論一下,因此,寫(xiě)下這些帖子,廖雪峰的課程連接在這里:廖雪峰
Python的相關(guān)介紹,以及它的歷史故事和運(yùn)行機(jī)制,可以參見(jiàn)這篇:python介紹
Python的安裝可以參見(jiàn)這篇:Python安裝
Python的運(yùn)行模式以及輸入輸出可以參見(jiàn)這篇:Python IO
Python的基礎(chǔ)概念介紹,可以參見(jiàn)這篇:Python 基礎(chǔ)
Python字符串和編碼的介紹,可以參見(jiàn)這篇:Python字符串與編碼
Python基本數(shù)據(jù)結(jié)構(gòu):list和tuple介紹,可以參見(jiàn)這篇:Python list和tuple
Python控制語(yǔ)句介紹:ifelse,可以參見(jiàn)這篇:Python 條件判斷
Python控制語(yǔ)句介紹:循環(huán)實(shí)現(xiàn),可以參見(jiàn)這篇:Python循環(huán)語(yǔ)句
Python數(shù)據(jù)結(jié)構(gòu):dict和set介紹Python數(shù)據(jù)結(jié)構(gòu)dict和set
Python函數(shù)相關(guān):Python函數(shù)
Python高階特性:Python高級(jí)特性
Python高階函數(shù):Python高階函數(shù)
Python匿名函數(shù):Python匿名函數(shù)
Python裝飾器:Python裝飾器
Python偏函數(shù):Python偏函數(shù)
Python模塊:Python模塊
Python面向?qū)ο缶幊?#xff08;1):Python面向?qū)ο?
Python面向?qū)ο缶幊?#xff08;2):Python面向?qū)ο?#xff08;2)
Python面向?qū)ο缶幊?#xff08;3):Python面向?qū)ο?#xff08;3)
Python面向?qū)ο缶幊?#xff08;4):Pyhton面向?qū)ο?#xff08;4)
Python面向?qū)ο蟾呒?jí)編程(上):Python面向?qū)ο蟾呒?jí)編程(上)
Python面向?qū)ο蟾呒?jí)編程(中上):Python面向?qū)ο蟾呒?jí)編程(中上)
Python面向?qū)ο蟾呒?jí)編程(中下):Python面向?qū)ο蟾呒?jí)編程(中下)
Python面向?qū)ο蟾呒?jí)編程(完):Python面向?qū)ο蟾呒?jí)編程(完)
Python錯(cuò)誤調(diào)試(起):Python調(diào)試:起
Python錯(cuò)誤調(diào)試(承):Python調(diào)試:承
Python錯(cuò)誤調(diào)試(轉(zhuǎn)):Python調(diào)試:轉(zhuǎn)
Python錯(cuò)誤調(diào)試(合):python調(diào)試:合
Python文件IO編程:Python文件IO
Python文件IO編程2:Python文件IO2
Python文件IO編程3:PYthon文件IO3
Python進(jìn)程和線程(起):Python進(jìn)程和線程起
Python進(jìn)程和線程(承):Python進(jìn)程和線程承
Python進(jìn)程和線程(轉(zhuǎn)):Python進(jìn)程和線程轉(zhuǎn)
Python進(jìn)程和線程(合):Python進(jìn)程和線程合
Python正則表達(dá)式:Python正則表達(dá)式
Python學(xué)習(xí)筆記:常用內(nèi)建模塊1:Python學(xué)習(xí)筆記:常用內(nèi)建模塊1
Python學(xué)習(xí)筆記:常用內(nèi)建模塊2:Python學(xué)習(xí)筆記:常用內(nèi)建模塊2
Python學(xué)習(xí)筆記:常用內(nèi)建模塊3:Python學(xué)習(xí)筆記:常用內(nèi)建模塊3
Python學(xué)習(xí)筆記:常用內(nèi)建模塊4:Python學(xué)習(xí)筆記: 常用內(nèi)建模塊4
Python學(xué)習(xí)筆記:常用內(nèi)建模塊5:Python學(xué)習(xí)筆記: 常用內(nèi)建模塊5
Python學(xué)習(xí)筆記:常用內(nèi)建模塊6:Python學(xué)習(xí)筆記:常用內(nèi)建模塊6
Python學(xué)習(xí)筆記:第三方模塊1:Python常用第三方模塊
Python學(xué)習(xí)筆記:第三方模塊2:Python常用第三方模塊
Python學(xué)習(xí)筆記:第三方模塊3:Python常用第三方模塊
Pytho學(xué)習(xí)筆記:網(wǎng)絡(luò)編程:Python網(wǎng)絡(luò)編程
Python學(xué)習(xí)筆記:電子郵件:Python電子郵件1
Python學(xué)習(xí)筆記:SMTP服務(wù)器:PythonSMTP服務(wù)器
Python學(xué)習(xí)筆記:POP3服務(wù)器:PythonPOP3服務(wù)器
Python學(xué)習(xí)筆記:Python數(shù)據(jù)庫(kù) Python數(shù)據(jù)庫(kù)1
Python學(xué)習(xí)筆記:Python數(shù)據(jù)庫(kù)2Python數(shù)據(jù)庫(kù)2
Python學(xué)習(xí)筆記:web開(kāi)發(fā)1Python學(xué)習(xí)筆記:web開(kāi)發(fā)1
Python學(xué)習(xí)筆記:web開(kāi)發(fā)2Python學(xué)習(xí)筆記: web開(kāi)發(fā)2
Python學(xué)習(xí)筆記: web開(kāi)發(fā)3Python學(xué)習(xí)筆記: web開(kāi)發(fā)3
Python學(xué)習(xí)筆記:異步IO(1)Python學(xué)習(xí)筆記:異步IO(1)
Python學(xué)習(xí)筆記:異步IO(2)Python學(xué)習(xí)筆記:異步IO(2)
Python學(xué)習(xí)筆記:異步IO(3)Python學(xué)習(xí)筆記:異步IO(3)
Python學(xué)習(xí)筆記:Day 1-2開(kāi)發(fā)Python學(xué)習(xí)筆記:Day1-2開(kāi)發(fā)
Python學(xué)習(xí)筆記:Day 3 ORMPython學(xué)習(xí)筆記:Day3 ORM
目錄
- 前言
- 目錄
- 編寫(xiě)Model
- 初始化數(shù)據(jù)庫(kù)表
- 編寫(xiě)數(shù)據(jù)訪問(wèn)代碼
編寫(xiě)Model
有了ORM,我們就可以把Web App需要的3個(gè)表用Model表示出來(lái):
import time, uuidfrom orm import Model, StringField, BooleanField, FloatField, TextFielddef next_id():return '%015d%s000' % (int(time.time() * 1000), uuid.uuid4().hex)class User(Model):__table__ = 'users'id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')email = StringField(ddl='varchar(50)')passwd = StringField(ddl='varchar(50)')admin = BooleanField()name = StringField(ddl='varchar(50)')image = StringField(ddl='varchar(500)')created_at = FloatField(default=time.time)class Blog(Model):__table__ = 'blogs'id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')user_id = StringField(ddl='varchar(50)')user_name = StringField(ddl='varchar(50)')user_image = StringField(ddl='varchar(500)')name = StringField(ddl='varchar(50)')summary = StringField(ddl='varchar(200)')content = TextField()created_at = FloatField(default=time.time)class Comment(Model):__table__ = 'comments'id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')blog_id = StringField(ddl='varchar(50)')user_id = StringField(ddl='varchar(50)')user_name = StringField(ddl='varchar(50)')user_image = StringField(ddl='varchar(500)')content = TextField()created_at = FloatField(default=time.time)在編寫(xiě)ORM時(shí),給一個(gè)Field增加一個(gè)default參數(shù)可以讓ORM自己填入缺省值,非常方便。并且,缺省值可以作為函數(shù)對(duì)象傳入,在調(diào)用save()時(shí)自動(dòng)計(jì)算。
例如,主鍵id的缺省值是函數(shù)next_id,創(chuàng)建時(shí)間created_at的缺省值是函數(shù)time.time,可以自動(dòng)設(shè)置當(dāng)前日期和時(shí)間。
日期和時(shí)間用float類型存儲(chǔ)在數(shù)據(jù)庫(kù)中,而不是datetime類型,這么做的好處是不必關(guān)心數(shù)據(jù)庫(kù)的時(shí)區(qū)以及時(shí)區(qū)轉(zhuǎn)換問(wèn)題,排序非常簡(jiǎn)單,顯示的時(shí)候,只需要做一個(gè)float到str的轉(zhuǎn)換,也非常容易。
初始化數(shù)據(jù)庫(kù)表
如果表的數(shù)量很少,可以手寫(xiě)創(chuàng)建表的SQL腳本:
-- schema.sqldrop database if exists awesome;create database awesome;use awesome;grant select, insert, update, delete on awesome.* to 'www-data'@'localhost' identified by 'www-data';create table users (`id` varchar(50) not null,`email` varchar(50) not null,`passwd` varchar(50) not null,`admin` bool not null,`name` varchar(50) not null,`image` varchar(500) not null,`created_at` real not null,unique key `idx_email` (`email`),key `idx_created_at` (`created_at`),primary key (`id`) ) engine=innodb default charset=utf8;create table blogs (`id` varchar(50) not null,`user_id` varchar(50) not null,`user_name` varchar(50) not null,`user_image` varchar(500) not null,`name` varchar(50) not null,`summary` varchar(200) not null,`content` mediumtext not null,`created_at` real not null,key `idx_created_at` (`created_at`),primary key (`id`) ) engine=innodb default charset=utf8;create table comments (`id` varchar(50) not null,`blog_id` varchar(50) not null,`user_id` varchar(50) not null,`user_name` varchar(50) not null,`user_image` varchar(500) not null,`content` mediumtext not null,`created_at` real not null,key `idx_created_at` (`created_at`),primary key (`id`) ) engine=innodb default charset=utf8;如果表的數(shù)量很多,可以從Model對(duì)象直接通過(guò)腳本自動(dòng)生成SQL腳本,使用更簡(jiǎn)單。
把SQL腳本放到MySQL命令行里執(zhí)行:
$ mysql -u root -p < schema.sql我們就完成了數(shù)據(jù)庫(kù)表的初始化。
編寫(xiě)數(shù)據(jù)訪問(wèn)代碼
接下來(lái),就可以真正開(kāi)始編寫(xiě)代碼操作對(duì)象了。比如,對(duì)于User對(duì)象,我們就可以做如下操作:
import orm from models import User, Blog, Commentdef test():yield from orm.create_pool(user='www-data', password='www-data', database='awesome')u = User(name='Test', email='test@example.com', passwd='1234567890', image='about:blank')yield from u.save()for x in test():pass可以在MySQL客戶端命令行查詢,看看數(shù)據(jù)是不是正常存儲(chǔ)到MySQL里面了。
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Python学习笔记:Day4 编写Model的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python学习笔记:Day 3编写OR
- 下一篇: Python学习笔记:Day5 编写we