大多数时候我们会使用以下方式用 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/