python manage.py shell 的增删改查

时间:2022-07-13 05:22:27

python manage.py shell 的增删改查

guoguo-MacBook-Pro:myblog guoguo$ python manage.py shell
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]:
#根据models.py 中导入相关模块
In [1]: from blog.models import Category,Tag,Post #存数据
In [2]: c = Category(name='category test1') In [3]: c.save() In [4]: t = Tag(name='tag test1') In [5]: t.save()
#查看数据<Category: category test1>] 新插入的数据
In [6]: Category.objects.all()
Out[6]: <QuerySet [<Category: category test>, <Category: category test1>]> In [7]: Tag.objects.all()
Out[7]: <QuerySet [<Tag: tag test>, <Tag: tag test1>]>
#修改数据
In [8]: c = Category.objects.get(name='category test1') In [9]: c.name = 'category test2' In [10]: c.save() In [11]: Category.objects.all()
Out[11]: <QuerySet [<Category: category test>, <Category: category test2>]> #删除数据
In [12]: p = Category.objects.get(name='category test2') In [13]: p
Out[13]: <Category: category test2> In [14]: p.delete()
Out[14]: (1, {'blog.Category': 1}) AttributeError: type object 'Category' has no attribute 'ojbects' In [16]: Category.objects.all()
Out[16]: <QuerySet [<Category: category test>]> In [17]: