如何使用peewee创建模型中定义的所有表 [英] How to create all tables defined in models using peewee

查看:478
本文介绍了如何使用peewee创建模型中定义的所有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用peewee定义了许多模型类. ClassName.create_table()可以生成表,但只能生成一个表.如何使用一个语句创建所有表?

I define a lot of model classes using peewee. ClassName.create_table() can generate the table,but only one table. How could I create all tables using a single statement?

推荐答案

假设所有表都分组在一个模块中,则扩展了coleifer的答案:

Extending coleifer's answer with the assumption that all tables are grouped in one module:

import inspect
import peewee
import tables
models = [
    obj for name, obj in inspect.getmembers(
        tables, lambda obj: type(obj) == type and issubclass(obj, peewee.Model)
    )
]
peewee.create_model_tables(models)

这篇关于如何使用peewee创建模型中定义的所有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆