Django 模型中的CRUD

一、通过 ORM 向 DB 中增加数据

1、Entry.objects.create(属性=值,属性=值)
Entry:具体要操作的Model类

ex:

  Author.objects.create(name='zsf',age=85)

2、创建一个 Models 对象,通过对象的 save() 完成增加
author = Author(names='laoshe',age=65)
author.save()

3、使用字典构建属性,通过 save() 完成增加
dic = {
'属性1':'值1',
'属性2':'值2',
}

obj = Entry(**dic)
obj.save()

二、查询操作

通过 Entry.objects 调用查询接口

1、查询所有对象
语法:all()
用法:Entry.objects.all()
返回:QuerySet

ex:
  Author.objects.all()
  等同于:select * from index_author

返回结果:
<QuerySet [<Author: Author object>, <Author: Author object>, <Author: Author object>]>

2、查询指定列
语法:values('列1','列2',...)
用法:Entry.objects.values('列1','列2',...)
返回:QuerySet

ex:
  Author.objects.values('names','age')
  等同于:select name,age from index_author

注意:
  values()可以用在所有的返回查询结果集的方法的后面

  Author.objects.all().values('names','age')

  <QuerySet [{'age': 65, 'names': 'ZhuZiqing'}, {'age': 68, 'names': 'laoshe'}, {'age': 59, 'names': 'MoYan'}]>

3、排序函数

语法:order_by('列1','列2')
用法:Entry.objects.order_by('-列1','列2')
默认的排序规则是升序
如果需要降序,则在列前添加一个 "-"
ex:
  1、Author.objects.order_by('age')
  2、Author.objects.all().order_by('-age');


4、对条件取反
语法:exclude()
用法:Entry.objects.exclude(条件)
ex:
  1、Author.objects.exclude(id=3)
  等同于:select * from author where not (id=3)
  2、Author.objects.exclude(id=3,age=85)
  等同于:select * from author where not (id=3 and age=85)

5、根据条件查询部分行数据(重难点)
方法:filter(参数)
用法:Entry.objects.filter(参数)
1、使用 Entry 中的属性作为查询参数
多个参数的话,使用 , 隔开,映射到sql语句上,是使用 and 来进行关联的
ex:
  1、Author.objects.filter(id=1)
  等同于:select * from author where id=1
  2、Author.objects.filter(id=1,name='莫言')
  等同于:select * from author where id=1 and name='莫言'
2、通过 Field Lookup(查询表达式)完成复杂条件的构建
查询表达式:每个查询表达式都是一个独立的查询条件,可以用在所有的有查询条件的位置处
  1、__exact
  作用:精确查询,等值判断
  用法:Entry.objects.filter(属性__exact=值)
  ex:
    Author.objects.filter(id__exact=1)
    select * from author where id=1
  2、__contains
  作用:筛选出属性中包含指定关键字的记录(模糊查询)
  ex:
    Author.objects.filter(names__contains='ao')
    select * from author where names like '%ao%'
  3、__lt
  作用:筛选出属性值小于指定值的记录
  4、__lte
  作用:筛选出属性值小于等于指定值的记录
  5、__gt
  作用:筛选出属性值大于指定值的记录
  6、__gte
  作用:筛选出属性值大于等于指定值的记录
  7、__startswith
  作用:筛选出以指定关键字开始的记录
  8、__endswith
  作用:筛选出以指定关键结尾的记录

6、查询只返回一条数据
语法:get(条件)
用法:Entry.objects.get(查询条件/表达式)
注意:
  该函数只适用于返回一条记录时使用

三、修改数据

1、修改单个数据
  1、通过 get() 得到要修改的实体对象
  2、通过实体对象的属性修改属性值
  3、再通过实体对象的save()保存回数据库
ex:
  au = Author.objects.get(id=1)
  au.names = "老舍"
  au.age = 45
  au.save()

2、批量修改数据
调用查询结果集的 update() 完成批量修改
Entry.objects.all().update(属性=值,属性=值)

ex:
  Author.objects.all().update(age=75)

四、删除数据
调用实体对象/查询结果集的 delete() 即可
1、删除单个对象
obj = Author.objects.get(id=1)
obj.delete()


2、删除多个对象
Author.objects.all().delete()

猜你喜欢

转载自www.cnblogs.com/hooo-1102/p/11956044.html