在Django shell中时,如何从数据库中删除列?

neh*_*iah 2 django django-models

这是场景:

  • 我无权访问psql提示。
  • 我确实可以访问python manage.py shellshell_plus

是否可以使用Django内部函数删除该数据库的列?我知道理论上是可能的。

欢迎任何见解。

neh*_*iah 5

正如Cezar所指出的那样,这不应该是一个挑战,就像python manage.py dbshell应该那样调用提示psql

如果没有其他效果,您仍然可以解决,因为Django允许从shell执行任意(原始)SQL。下面的代码片段应该做到这一点:

from django.db import connection
cursor = connection.cursor()
cursor.execute("alter table table_name DROP column column_name")
Run Code Online (Sandbox Code Playgroud)