python peewee插入,表名在变量中

标签 python peewee

大多数时候我们会使用以下方式用 peewee 创建项目:

User.create(name='aa',age=20, ...)

但在某些情况下,表名称存储在变量中。我注意到我可以使用database.execute_sql()来执行原始SQL语句。但我觉得有点乱。

有没有办法做这样的事情?

#{TableName}.create(name='aa',age=20, ...)

感谢您的宝贵时间!

最佳答案

使用概述的方法here :

class Table(Model):
    text = TextField()

    class Meta:
        database = DB

table_name = 'Table'
table = globals()[table_name].create(text='lorem ipsum')

关于python peewee插入,表名在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46725257/

相关文章:

python - 为什么列表理解在转置矩阵中胜过 zip

Python:基于nosetest是否运行的条件变量

python - 将 PyQt 与 sql orm 库(sqlalchemy、peewee 等)集成

python - 使用 Python Peewee 指定要连接的表

python - 如何合并两个表以在 peewee 中选择分页数据?

python - 使用 COM 自动化服务器和 PyQt

python - 列表理解——将一个列表中的字符串转换为另一个列表中的整数

python - 如何处理 keras LSTM 的输入和输出形状

python - OSMNX - 尝试在控制台中绘制图表但没有任何运气

python - 使用 Peewee 追溯创建索引?