針對Django中用Python3 manage.py shell來操作表的一些操作:
(前提要完成數據庫遷移命令)
首先在Django,即Pycharm中自帶的Terminal中輸入Python3 manage.py shell
既可開啟shell模式,接下來就可以直接對表模型直接進行操作了
示例:新增數據操作
models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
Terminal窗口
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
F:\code\drf_serializer>python3 manage.py shell
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app01 import models
# 基於queryset對象新增數據的操作
>>> models.Author.objects.create(name="zdc", age=18)
<Author: Author object>
# 基於對象新增數據的操作
>>> author_obj = models.Author(name="wjw", age=18)
>>> author_obj.save()
總結:
語法操作其實與Django中ORM的操作無異,只是無需啟動django,配置url發送請求才能操作數據了,方便了開發人員的調試