1. 程式人生 > >python 編寫Model

python 編寫Model

好處 寫代碼 schema 並且 表示 targe mode local ref

有了ORM,我們就可以把Web App需要的3個表用Model表示出來:

import time, uuid

from orm import Model, StringField, BooleanField, FloatField, TextField

def 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)

在編寫ORM時,給一個Field增加一個default參數可以讓ORM自己填入缺省值,非常方便。並且,缺省值可以作為函數對象傳入,在調用save()時自動計算。

例如,主鍵id的缺省值是函數next_id,創建時間created_at的缺省值是函數time.time,可以自動設置當前日期和時間。

日期和時間用float類型存儲在數據庫中,而不是datetime類型,這麽做的好處是不必關心數據庫的時區以及時區轉換問題,排序非常簡單,顯示的時候,只需要做一個floatstr的轉換,也非常容易。

初始化數據庫表

如果表的數量很少,可以手寫創建表的SQL腳本:

-- schema.sql

drop 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;

如果表的數量很多,可以從Model對象直接通過腳本自動生成SQL腳本,使用更簡單。

把SQL腳本放到MySQL命令行裏執行:

$ mysql -u root -p < schema.sql

我們就完成了數據庫表的初始化。

編寫數據訪問代碼

接下來,就可以真正開始編寫代碼操作對象了。比如,對於User對象,我們就可以做如下操作:

import orm
from models import User, Blog, Comment

def test():
    yield from orm.create_pool(user=‘www-data‘, password=‘www-data‘, database=‘awesome‘)

    u = User(name=‘Test‘, email=[email protected]
/* */, passwd=‘1234567890‘, image=‘about:blank‘) yield from u.save() for x in test(): pass

可以在MySQL客戶端命令行查詢,看看數據是不是正常存儲到MySQL裏面了。

http://www.pdfxs.com/cpgjhegfgncpcfefdfcfdieecfdjebcfefdfcfecedcfdidicfefdicfebefcfeceb/

python 編寫Model