Loading

django | 根据 model 创建对应的表

代码如下:

   from django.db import connections

   with connections['db_to_create_a_table_in'].schema_editor() as schema_editor:
        schema_editor.create_model(YourUnmanagedModelClass)

可通过错误捕获实现 create table if not exist 的效果。

Reference

  1. SchemaEditor | Django documentation | Django (djangoproject.com)
  2. python - How to programmatically generate the CREATE TABLE SQL statement for a given model in Django? - Stack Overflow
posted @ 2024-06-25 22:28  kingron  阅读(3)  评论(0编辑  收藏  举报