一、前言

  之前我们也学习了,关于django的一些数据库的基本操作,今天我们来完善一下,我们今天来学习一下model的基本操作和进阶操作,顺便来巩固一下。

二、Model基本操作

# 增
#
# models.Tb1.objects.create(c1='xx', c2='oo')  增加一条数据,可以接受字典类型数据 **kwargs

# obj = models.Tb1(c1='xx', c2='oo')
# obj.save()

# 查
#
# models.Tb1.objects.get(id=123)         # 获取单条数据,不存在则报错(不建议)
# models.Tb1.objects.all()               # 获取全部
# models.Tb1.objects.filter(name='seven') # 获取指定条件的数据
# models.Tb1.objects.exclude(name='seven') # 获取指定条件的数据

# 删
#
# models.Tb1.objects.filter(name='seven').delete() # 删除指定条件的数据

# 改
# models.Tb1.objects.filter(name='seven').update(gender='0')  # 将指定条件的数据更新,均支持 **kwargs
# obj = models.Tb1.objects.get(id=1)
# obj.c1 = '111'
# obj.save()                                                 # 修改单条数据

二、进阶操作

 1 # 获取个数
 2 #
 3 # models.Tb1.objects.filter(name='seven').count()
 4  
 5 # 大于,小于
 6 #
 7 # models.Tb1.objects.filter(id__gt=1)              # 获取id大于1的值
 8 # models.Tb1.objects.filter(id__gte=1)              # 获取id大于等于1的值
 9 # models.Tb1.objects.filter(id__lt=10)             # 获取id小于10的值
10 # models.Tb1.objects.filter(id__lte=10)             # 获取id小于10的值
11 # models.Tb1.objects.filter(id__lt=10, id__gt=1)   # 获取id大于1 且 小于10的值
12  
13 # in
14 #
15 # models.Tb1.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据
16 # models.Tb1.objects.exclude(id__in=[11, 22, 33])  # not in
17  
18 # isnull,是否为空
19 # Entry.objects.filter(pub_date__isnull=True)  #不能写成pub_date=null,只能写成pub_date__isnull=True
20  
21 # contains
22 #
23 # models.Tb1.objects.filter(name__contains="ven")
24 # models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
25 # models.Tb1.objects.exclude(name__icontains="ven")
26  
27 # range
28 #
29 # models.Tb1.objects.filter(id__range=[1, 2])   # 范围bettwen and
30  
31 # 其他类似
32 #
33 # startswith,istartswith, endswith, iendswith,
34  
35 # order by
36 #
37 # models.Tb1.objects.filter(name='seven').order_by('id')    # asc
38 # models.Tb1.objects.filter(name='seven').order_by('-id')   # desc
39  
40 # group by
41 #
42 # from django.db.models import Count, Min, Max, Sum
43 # models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
44 # SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
45  
46 # limit 、offset
47 #
48 # models.Tb1.objects.all()[10:20]
49  
50 # regex正则匹配,iregex 不区分大小写
51 #
52 # Entry.objects.get(title__regex=r'^(An?|The) +')
53 # Entry.objects.get(title__iregex=r'^(an?|the) +')
54  
55 # date
56 #
57 # Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
58 # Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
59  
60 # year
61 #
62 # Entry.objects.filter(pub_date__year=2005)
63 # Entry.objects.filter(pub_date__year__gte=2005)
64  
65 # month
66 #
67 # Entry.objects.filter(pub_date__month=12)
68 # Entry.objects.filter(pub_date__month__gte=6)
69  
70 # day
71 #
72 # Entry.objects.filter(pub_date__day=3)
73 # Entry.objects.filter(pub_date__day__gte=3)
74  
75 # week_day
76 #
77 # Entry.objects.filter(pub_date__week_day=2)
78 # Entry.objects.filter(pub_date__week_day__gte=2)
79  
80 # hour
81 #
82 # Event.objects.filter(timestamp__hour=23)
83 # Event.objects.filter(time__hour=5)
84 # Event.objects.filter(timestamp__hour__gte=12)
85  
86 # minute
87 #
88 # Event.objects.filter(timestamp__minute=29)
89 # Event.objects.filter(time__minute=46)
90 # Event.objects.filter(timestamp__minute__gte=29)
91  
92 # second
93 #
94 # Event.objects.filter(timestamp__second=31)
95 # Event.objects.filter(time__second=2)
96 # Event.objects.filter(timestamp__second__gte=31)
进阶操作

相关文章:

  • 2022-12-23
  • 2021-08-03
  • 2021-06-12
  • 2021-11-20
  • 2021-08-01
  • 2021-09-24
  • 2022-02-08
  • 2021-11-30
猜你喜欢
  • 2022-02-13
  • 2022-12-23
  • 2021-11-03
  • 2021-07-04
  • 2022-12-23
  • 2021-10-19
  • 2021-10-07
相关资源
相似解决方案