Open PyxYuYu opened 8 years ago
Prosperity discovers vice, adversity virtue. 0x01 Django
Prosperity discovers vice, adversity virtue.
Build a Blog Application
Creating objects
python manage.py shell
Python shell
>>> from django.contrib.auth.models import User >>> from blog.models import Post >>> user = User.objects.get(username='admin') >>> post = Post.objects.creat(title='One more post', slug='one-more-post', body='Post body.', author=user) >>> post.save()
User.objects.get()
get()
create()
Post()
post = Post(title = 'Another post', slug='another-post', body='Post body.', author=user)
post.save()
Updating objects
>>> post.title = 'New title' >>> post.save()
你好: 我做这个练习时,输入post.save() 按回车之后总显示错误信息: 不明白是哪里出错了,可以请教一下吗? 谢谢你
Build a Blog Application
Creating objects
python manage.py shell
打开Python shell
User.objects.get()
中get()
方法从数据库中获取一个对象,如果没有这个对象,会报异常,如果获取不是唯一的对象(多个),也会报异常create()
方法,也可以直接用Post()
创建post = Post(title = 'Another post', slug='another-post', body='Post body.', author=user)
post.save()
保存对象到数据库Updating objects