Python实战-编写Web App-Day4-编写Model

Python实战 木人张 7个月前 (04-12) 56次浏览 0个评论 扫描二维码
文章目录[隐藏]

编写Model

前面编写了ORM,现在可以把webapp需要的3个表用Model写出来:

# -*- coding: UTF-8 -*-
# Filename : models.py
# author by : www.woodmanzhang.com


import time, uuid

from orm import Model, StringField, BooldeanField, 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 = BooldeanField()
    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(50)')
    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(50)')
    content = TextField()
    created_at = FloatField(default=time.time)


给一个Field增加一个default参数可以让ORM自己填入缺省值。缺省值可以作为函数对象传入,在调用save()时自动计算。
日期和时间用float类型存储在数据库中,而不是datetime类型,这么做的好处是不必关心数据库的时区以及时区转换问题,排序非常简单,显示的时候,只需要做一个float到str的转换,也非常容易。

数据库初始化

这里我们使用navicat管理mysql
1建立与本地mysql的服务


2创建一个新的数据库woodman



3写一个创建表的sql脚本

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;

4运行sql文件创建表



ok,数据库表初始化完成,接下来测试下

数据库访问测试

对于user对象,数据库操作测试:

import asyncio
import orm
from models import User, Blog, Comment

loop = asyncio.get_event_loop()
async def test():
    await orm.create_pool(user='root', password='password', db='woodman', loop=loop)

    u = User(name='Test', email='[email protected]', passwd='1234567890', image='about:blank')

    await u.save()

loop.run_until_complete(test())

到navicat看下效果


木人张,版权所有丨如未注明 , 均为原创,禁止转载。
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址