创建Model(4)-使用Model API操作数据

使用python manage.py shell而不是简单地使用python进入命令行,是因为前者可以import task_project项目下的module,而后者不能。

D:\sort\svn_labreport\task_project>python manage.py shell
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
In [1]: 

查看当前项目中有哪些任务

In [1]: from task_tool.models import Task

In [2]: from django.utils import timezone

In [3]: Task.objects.all()
Out[3]: , , , , , , , , , , ]>

创建新的任务

In [4]: t = Task(title='test task 1')

In [5]: t
Out[5]: 

In [6]: t.save()

查看刚创建的项目的其他属性

In [7]: t.create_date
Out[7]: datetime.datetime(2017, 7, 25, 7, 25, 53, 803000, tzinfo=)

In [8]: t.update_date
Out[8]: datetime.datetime(2017, 7, 25, 7, 25, 53, 803000, tzinfo=)

In [9]: t.deadline
Out[9]: u''

In [10]: t.file
Out[10]: 

In [11]: t.content
Out[11]: u''

In [12]: t.id
Out[12]: 102

修改该任务的标题属性

In [13]: t.title = "new title name for test task 1"

In [14]: t.title
Out[14]: 'new title name for test task 1'

In [15]: t.save()

In [16]: Task.objects.all()
Out[16]: , , , , , , , , , , ,
 ]>

你可能感兴趣的:(创建Model(4)-使用Model API操作数据)