Django(part23)--Django shell的使用

学习笔记,仅供参考,有错必纠


数据库的操作(CRUD操作)


Django shell 的使用


在Django提供了一个交互式的操作项目叫Django Shell 它能够在交互模式用项目工程的代码执行相应的操作,利用Django Shell 可以代替编写View的代码来进行直接操作,在Django Shell只能进行简单的操作,不能运行远程调式


Django shell的启动方式:

python manage.py shell

现在,我们基于上一个BLOG的项目创建数据对象,使用Django shell。


在cmd中启动Django shell:

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_db>python manage.py sh
ell
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 6
4 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 5.1.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.

我们在cmd中敲入下面这些代码:

In [1]: from bookstore import models
In [2]: models.Book.objects.create(title="R", pub = "人民邮电出版社")
Out[2]: <Book: Book object (3)>

查看数据库中的bookstore_book数据表:

mysql> select * from bookstore_book;
+----+-------------------+----------------+
| id | title             | pub            |
+----+-------------------+----------------+
|  1 | Djangoweb开发实战 | 清华大学出版社 |
|  2 | python            | 机械工业出版社 |
|  3 | R                 | 人民邮电出版社 |
+----+-------------------+----------------+
3 rows in set (0.00 sec)

再向cmd敲入以下代码:

In [1]: from bookstore import models
In [3]: models.Author.objects.create(name="山羊", age = 23,
   ...: email = "[email protected]")
Out[3]: <Author: Author object (1)>

查看数据库中的bookstore_book数据表:

mysql> select * from bookstore_author;
+----+------+-----+----------------------+
| id | name | age | email                |
+----+------+-----+----------------------+
|  1 | 山羊 |  23 | [email protected] |
+----+------+-----+----------------------+
1 row in set (0.00 sec)

嗯!记录已更新。

猜你喜欢

转载自blog.csdn.net/m0_37422217/article/details/106879080
今日推荐