在 Django 的 View 中调用 syncdb

K*K posted @ Wed, 27 Mar 2013 19:24:43 +0800 in 未归类 with tags django view syncdb , 2053 readers

完全吃不消 SAE 的 MySQL 各种限制了,无数次地清空数据库 -> 重新初始化 -> 导入 SQL 文件。

为什么不能在 Django 中建立好 Model 后,直接从 Django 的 View 里 syncdb 呢。

 

from django.http import HttpResponse

def syncdb(request):
    from django.core.management import call_command
    from cStringIO import StringIO
    import sys
    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    mystdout.write('<pre>')
    call_command('syncdb')
    mystdout.write('</pre>')
    sys.stdout = old_stdout
    mystdout.seek(0)
    return HttpResponse(mystdout.read())

什么时候再把 South 整合进去就完美啦~~~ ;-)

  • No match
  • No match

Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter