如何在Heroku中为Django应用程序删除单个表

fan*_*err 2 django heroku

我看到如何在Django中删除整个数据库(在Heroku上销毁Postgres数据库).但是,我想删除几个表(基本上在'python manage.py sqlclear appname'中执行sql命令).我该怎么做呢?

Hac*_*ife 5

嘿,这很直接.您应该只创建一个可以处理原始SQL的视图功能.我昨天试图解决postgres唯一索引同步问题.

Views.py:将此映射到某个URL,然后访问浏览器中的url,该函数将执行.

from django.db import connection, transaction

def dropTable(request):

 cursor = connection.cursor()

 cursor.execute(“DROP TABLE WHATEVER”) //custom raw SQL goes here

 success = simplejson.dumps({‘success’:’success’,}) //I do this as a success message

 return HttpResponse(success, mimetype=’application/json’) //you'll need an import or two for the json stuff to work
Run Code Online (Sandbox Code Playgroud)

  • 我认为您还应该在上面的答案中添加用于将更改提交到DB的语句:`transaction.commit_unless_managed()` (2认同)