How to create all tables defined in models using peewee

前端 未结 6 1654
小鲜肉
小鲜肉 2021-02-20 08:07

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 state

6条回答
  •  萌比男神i
    2021-02-20 08:48

    This snipnet will create all tables which objects are defined in the current module:

    import sys
    
    for cls in sys.modules[__name__].__dict__.values():
        try:
            if BaseModel in cls.__bases__:
                cls.create_table()
        except:
            pass
    

提交回复
热议问题