相关文章推荐
发呆的花生  ·  天野繪里香 - 萌娘百科 萬物皆可萌的百科全書·  11 月前    · 
知识渊博的硬币  ·  十二週有氧運動結合彈力帶阻力訓練對慢性阻塞性 ...·  1 年前    · 
失恋的铁板烧  ·  第一性原理 - 搜狗百科·  1 年前    · 
从容的鼠标  ·  点进来感受血压升高:为什么所有网络流行语的尽 ...·  1 年前    · 
逆袭的葡萄酒  ·  神奇宝贝天狂传说全部 - 腾讯动漫·  1 年前    · 
Code  ›  已有数据库情况下使用Python进行ORM操作开发者社区
python 大数据 云数据库 orm
https://cloud.tencent.com/developer/article/1892303
长情的小熊猫
3 年前
作者头像
zx钟
0 篇文章

已有数据库情况下使用Python进行ORM操作

前往专栏
腾讯云
备案 控制台
开发者社区
学习
实践
活动
专区
工具
TVP
文章/答案/技术大牛
写文章
社区首页 > 专栏 > 测试游记 > 正文

已有数据库情况下使用Python进行ORM操作

发布 于 2021-10-21 16:58:06
460 0
举报

现状

在实际测试中发现,需要插入大量的测试数据或者有依赖关系的数据 来达到测试的目的,这时我们可以使用python来简化和规范化该操作。

采用 peewee 来将SQL代码转化为Python代码实现ORM

peewee 是一个轻量级的 python ORM 库。内建对 SQLite、 MySQL 和 PostgreSQL 的支持

peewee文档地址:

http://docs.peewee-orm.com/en/latest/

中文版文档:

https://www.osgeo.cn/peewee/

生成数据模型

在安装peewee后可以在命令行中输入指令,快速的将已有的 数据库 转化成数据模型

python -m pwiz -e 数据库类型 -p 端口 -u 账号 -H IP地址 --password 密码 -o -i 库名 > 生成的py文件名.py

例如

python -m pwiz -e mysql -p 3306 -u doctor_core -H 数据库ip --password 数据库密码 -o -i 数据库账号 > 导出的文件.py

查看doctor_info表

-- auto-generated definition
create table doctor_info
    id                      bigint(22) unsigned auto_increment comment '主键'
        primary key,
    user_id                 bigint unsigned                not null comment '用户编号',
    login_id                varchar(128)                   null comment '登录名',
    mobile_no               varchar(11)                    null comment '手机号码',
    telephone_no            varchar(50)                    null comment '医生固话',
    title_name              varchar(200)                   null comment '行政职称名',
    dept_uuid               varchar(40)                    null comment '科室uuid',
    dept_name               varchar(100)                   null comment '科室名称',
    hospital_uuid           varchar(40)                    null comment '医院uuid',
    hospital_name           varchar(100)                   null comment '医院名称',
    property                tinyint(1) unsigned            null comment '医院性质',
    expert_uuid             varchar(45)                    null comment '专家uuid',
    expert_name             varchar(40)                    null comment '专家姓名',
    favorite_dept           varchar(1000)                  null comment '医生擅长科室,多个以英文逗号分隔',
    job_number              varchar(40)                    null comment '医院端医生工号',
    canal_online            tinyint(1) unsigned default 1  null comment '渠道:1为线上,0为线下,2为兼职,3为赠险注册',
    source                  bigint(11) unsigned            null comment '应用来源',
    reg_type                tinyint(2) unsigned            not null comment '注册来源: ',
    state                   tinyint(4) unsigned default 0  not null comment '状态,0:未认证,1:已认证',
    is_close                tinyint(1) unsigned default 1  not null comment ' 0,关闭,1,开通',
    is_deleted              tinyint unsigned    default 0  not null comment '是否删除',
    gmt_created             datetime                       not null comment '创建时间',
    gmt_modified            datetime                       not null comment '修改时间',
    project_source_id       int                            null comment '对应项目来源表的外键',
    source_group_id         bigint                         null comment '来源分组',
    source_group_name       varchar(200)                   null comment '来源分组名称',
    role_id                 varchar(100)                   null comment '医生关联角色',
    grade                   tinyint(3)          default 0  null comment '医生资质等级',
    is_special              tinyint(2)          default 0  null comment '是否特批医生',
    relation_state          tinyint(4) unsigned            null comment '关联状态',
    relation_remark         varchar(256)                   null comment '关联备注',
    gmt_relation            datetime                       null comment '关联时间',
    is_auto_relation        tinyint(1) unsigned            null comment '是否自动关联',
    supervise_record_status tinyint(2) unsigned default 0  not null comment '监管备案状态',
    supervise_record_remark varchar(255)        default '' not null comment '监管备案失败原因',
    constraint uniq_user_id
        unique (user_id)
    comment '医生信息表' charset = utf8;
create index idx_expert_name
    on doctor_info (expert_name);
create index idx_expert_uuid
    on doctor_info (expert_uuid);
create index idx_gmtCreated
    on doctor_info (gmt_created);
create index idx_gmtModified
    on doctor_info (gmt_modified);
create index idx_hospital_uuid_job_number
    on doctor_info (hospital_uuid, job_number);
create index idx_job_number
    on doctor_info (job_number);
create index idx_loginId
    on doctor_info (login_id);
create index idx_mobile_no
    on doctor_info (mobile_no);

查看它转换后的代码为:

class DoctorInfo(BaseModel):
    id = BigAutoField()
    user_id = BigIntegerField(unique=True)
    login_id = CharField(index=True, null=True)
    mobile_no = CharField(index=True, null=True)
    telephone_no = CharField(null=True)
    title_name = CharField(null=True)
    dept_uuid = CharField(null=True)
    dept_name = CharField(null=True)
    hospital_uuid = CharField(null=True)
    hospital_name = CharField(null=True)
    property = IntegerField(null=True)
    expert_uuid = CharField(index=True, null=True)
    expert_name = CharField(index=True, null=True)
    favorite_dept = CharField(null=True)
    job_number = CharField(index=True, null=True)
    canal_online = IntegerField(constraints=[SQL("DEFAULT 1")], null=True)
    source = BigIntegerField(null=True)
    reg_type = IntegerField()
    state = IntegerField(constraints=[SQL("DEFAULT 0")])
    is_close = IntegerField(constraints=[SQL("DEFAULT 1")])
    is_deleted = IntegerField(constraints=[SQL("DEFAULT 0")])
    gmt_created = DateTimeField(index=True)
    gmt_modified = DateTimeField(index=True)
    project_source_id = IntegerField(null=True)
    source_group_id = BigIntegerField(null=True)
    source_group_name = CharField(null=True)
    role_id = CharField(null=True)
    grade = IntegerField(constraints=[SQL("DEFAULT 0")], null=True)
    is_special = IntegerField(constraints=[SQL("DEFAULT 0")], null=True)
    relation_state = IntegerField(null=True)
    relation_remark = CharField(null=True)
    gmt_relation = DateTimeField(null=True)
    is_auto_relation = IntegerField(null=True)
    supervise_record_status = IntegerField(constraints=[SQL("DEFAULT 0")])
    supervise_record_remark = CharField(constraints=[SQL("DEFAULT ''")])
    class Meta:
        table_name = 'doctor_info'
        indexes = (
            (('hospital_uuid', 'job_number'), False),

我们使用这段代码来完成一次CRUD操作

增

DoctorInfo(user_id=xx,login_id=xx,....).save()

删

DoctorInfo.delete().where(DoctorInfo.expert_name == '张三').execute()

改

DoctorInfo.expert_name = '李四'
 
推荐文章
发呆的花生  ·  天野繪里香 - 萌娘百科 萬物皆可萌的百科全書
11 月前
知识渊博的硬币  ·  十二週有氧運動結合彈力帶阻力訓練對慢性阻塞性肺疾病患者的影響__臺灣博碩士論文知識加值系統
1 年前
失恋的铁板烧  ·  第一性原理 - 搜狗百科
1 年前
从容的鼠标  ·  点进来感受血压升高:为什么所有网络流行语的尽头都是阴阳怪气? | 人人都是产品经理
1 年前
逆袭的葡萄酒  ·  神奇宝贝天狂传说全部 - 腾讯动漫
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号