编写Model

结合上文编写完ORM,我们就可以把博客中需要用到的Uers,blogs以及comments各自的信息,存储到数据库的三个表中,并透过使用Model表示出来。

编写代码如下:

#!usr/bin/env python
# -*- coding: utf-8 -*-

\'\'\'
创建model
\'\'\'

__author__=\'Seiei\'

import uuid,time
import asyncio
from web_app import orm
from web_app.orm import Model,StringField,IntegerField,BooleanField,TextField,FloatField
#from orm import Model,StringField,IntegerField,BooleanField,TextField,FloatField

def next_id():#随机生成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)\')#注意由于是image,所以这里接受字节是500
    create_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()
    create_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()
    create_at = FloatField(default=time.time)


if __name__== \'__main__\':

    async def test():
        await orm.create_pool(loop,user=\'www-data\', password=\'www-data\', db=\'awesome\')
        u = User(name=\'Test\', email=\'test@example.com\', passwd=\'123456780\', image=\'about:blank\')
        await u.save()
        a = await u.findall() #这个要打印才显示出来
        print(a)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(test())
    orm.__pool.close()  #在关闭event loop之前,首先需要关闭连接池。
    loop.run_until_complete(orm.__pool.wait_closed())#在关闭event loop之前,首先需要关闭连接池。
    loop.close()

在编写完Model代码后,我自行添加了数据访问代码,试着检测一下所写ORM以及Model是否有误。
但在此之前,得先初始化数据库表

初始化数据库表

我是直接使用Python中的mysql模块对数据库进行操作。教程可看廖老师的,或看详细的官方文档

代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = \'Seiei\'

import mysql.connector

conn = mysql.connector.connect(user=\'root\', password=\'password\', database=\'awesome\')#连接MySQL数据库中的awesome数据库
cursor = conn.cursor()#创建游标
cursor.execute(\'create table users (id varchar(50) primary key,email varchar(50),passwd varchar(50),name varchar(50),image varchar(500),admin boolean,create_at real)\')#创建users表-->表列都要定义名字及类型,主键后还要跟primary key
cursor.execute(\'create table blogs (id varchar(50) primary key,user_id varchar(50),user_name varchar(50),user_image varchar(500),name varchar(50),summary varchar(200),content text,create_at real)\')#创建blogs表
cursor.execute(\'create table comments (id varchar(50) primary key,blog_id varchar(50),user_id varchar(50),user_name varchar(50),user_image varchar(500),content text,create_at real)\')#创建comments表
cursor.close()
conn.commit()
conn.close()

版权声明:本文为AB786883603原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/AB786883603/p/8325446.html