python web开发中django数据库操作

栏目: Python · 发布时间: 6年前

内容简介:数据库操作是我们经常要用到的,可以说没有数据库的网站是没有灵魂的,而我们要做一个有灵魂的网站,就需要学习数据库,今天讲下django中数据库的增删改查基本操作。1数据库的增加b = Blog(name="梦回故里",content="一个有趣的灵魂")

数据库操作是我们经常要用到的,可以说没有数据库的网站是没有灵魂的,而我们要做一个有灵魂的网站,就需要学习数据库,今天讲下django中数据库的增删改查基本操作。

1数据库的增加

b = Blog(name="梦回故里",content="一个有趣的灵魂")

b.save()

2.数据库的查找

查找全部:

blogs = Blog.objects.all()

条件查找:

blogs  =Blog.objects.filter(name='梦回故里')

主键查找:

blog = Blog.objects.get(pk = 1)

limit查找:

blogs = Blog.objects.all()[5:10]

order顺序查找:

blog = Blog.objects.order_by('name')[0]

表达式查找:

blogs = Blog.objects.filter(pub_date__lte='2019-04-04')

不区分大小写:

blogs = Blog.objects.get(name__iexact='Tom Fly')

模糊查询:

blogs = Blog.objects.get(name__contains='Meng')

3.数据库的修改

b = Blog.objects.get(name="梦回故里")

b.name = "电影宇宙"

b.save()

4.数据库的删除

b = Blog.objects.filter(name='梦回故里')

b.delete()

b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')>>> b.save()

以上所述就是小编给大家介绍的《python web开发中django数据库操作》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Lighttpd

Lighttpd

Andre Bogus / Packt Publishing / 2008-10 / 39.99

This is your fast guide to getting started and getting inside the Lighttpd web server. Written from a developer's perspective, this book helps you understand Lighttpd, and get it set up as securely an......一起来看看 《Lighttpd》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具