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 @   kingron  阅读(10)  评论(0编辑  收藏  举报
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示