python manage.py shell 的增刪改查


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]: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM