下文的project1爲project名,app1爲app名
在django project 下執行:python manage.py shellpython
# 能夠在Python console裏執行Python命令或者包含django的命令 > from app1.models import User > user = User.objects.get(name="zhangsan") > user.name = "zhangsan01" > user.save()
# 若是要執行的腳本不在django project的根目錄下記得修改path import sys sys.path.append("/d1/cmdb/cmdb2") #引入django project環境 import django import os os.environ.setdefault('DJANGO_SETTINGS_MODULE','project1.settings') django.setup() #代碼邏輯 from app1.models import User user = User.objects.get(name="zhangsan") user.name = "zhangsan01" user.save()
而後python script.py 執行shell