How to create all tables defined in models using peewee

前端 未结 6 1656
小鲜肉
小鲜肉 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条回答
  •  耶瑟儿~
    2021-02-20 08:54

    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)
    

提交回复
热议问题