Django-extensions 可谓是 Django 开发利器。
pip install django-extensions #其他方法 Google
INSTALLED_APPS = (
...
'django_extensions',
)
====================== 不是很完美的分割线 =====================
$ ./manage.py shell_plus --ipython #使用 ipython shell
$ ./manage.py shell_plus --bpython #使用 bpython shell
$ ./manage.py shell_plus --ptpython #使用 ptpython shell
$ ./manage.py shell_plus --plain #当然也可以是纯 python, 不过这个是不是有点傻!!!
不过可以在 django settings.py 加入以下配置
SHELL_PLUS = "ptpython"
====================== 不是很完美的分割线 =====================
$ mkdir scripts
$ touch scripts/__init__.py
$ touch scripts/delete_all_questions.py
# scripts/delete_all_questions.py
from polls.models import Question
def run():
# Fetch all questions
questions = Question.objects.all()
# Delete questions
questions.delete()
$ python manage.py runscript delete_all_questions
$ python manage.py runscript delete_all_questions --traceback # 开启 Debug 模式
写在最后, 更多使用可以参考官方文档