Python3 manage.py shell how to use?

For Django with Python3 manage.py shell to manipulate some of the operating table:

(To complete the premise database migration command)

Python3 manage.py shell is first input in Django, i.e. comes in the Terminal Pycharm

Can open a shell model, then the model can directly operate directly on the table

Example: data addition operation

models.py

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()

Terminal window

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()

to sum up:

语法操作其实与Django中ORM的操作无异,只是无需启动django,配置url发送请求才能操作数据了,方便了开发人员的调试

Guess you like

Origin www.cnblogs.com/shuchengyi/p/11139598.html