How to create all tables defined in models using peewee

前端 未结 6 1672
小鲜肉
小鲜肉 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 09:01

    def create_all_tables():
        for cls in sys.modules[__name__].__dict__.values():
            if hasattr(cls, '__bases__') and issubclass(cls, peewee.Model):
                if cls is not BaseModel:
                    cls.create_table()
    

    This also works for ManyToManyField's get_through_model().

提交回复
热议问题