1

Most of time we'll use following way to create items with peewee:

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

But in some circumstances the Table name is stored in a variable. I notice I can use database.execute_sql() to execute raw SQL statement. But I think it's a little messy.

Is there a way to do something like this?

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

Thanks for your time!

mCY
  • 2,731
  • 7
  • 25
  • 43

1 Answers1

3

Using the approach outlined here:

class Table(Model):
    text = TextField()

    class Meta:
        database = DB

table_name = 'Table'
table = globals()[table_name].create(text='lorem ipsum')
Henry Heath
  • 1,072
  • 11
  • 19