<强> 1如何在做ORM查询时查看SQl的执行情况强>
(1)最底层的django.db。连接
在django壳中使用,python管理。py壳
在在在django。数据库导入连接 在在在Books.objects.all () 在在在连接。查询# #可以查看查询时间 [{“sql”:选择“testsql_books”。”id”、“testsql_books”。“名称”、“testsql_books”。“author_id”从“testsql_books”里美 T 21”、“时间”:“0.002”}]
(2) django-extensions插件,
pip安装django-extensions
INSTALLED_APPS=( … “django_extensions”, … )
在django壳中使用,python管理。py shell_plus ——print-sql(扩展强化)
这样每次查询都会有sql输出
在在在从testsql。模型导入的书 在在在Books.objects.all () 选择“testsql_books”。”id”、“testsql_books”。“名称”、“testsql_books”。“author_id”从“testsql_books”限制21 执行时间:0.002000秒(数据库:默认) & lt; QuerySet [& lt;书:书籍object> & lt;书:图书object>, & lt;书:图书object>]比; >之前<强> 2 ORM查询操作以及优化强>
基本操作
增 models.Tb1.objects。创建(c1=' xx ' c2=皁o”)增加一条数据,可以接受字典类型数据* * kwargs obj=模型。Tb1 (c1=' xx ', c2=皁o”) obj.save () 查 models.Tb1.objects.get (id=123) #获取单条数据,不存在则报错(不建议) models.Tb1.objects.all() #获取全部 models.Tb1.objects.filter (name=' 7 ') #获取指定条件的数据 models.Tb1.objects.exclude (name=' 7 ') #获取指定条件的数据 删 models.Tb1.objects.filter (name=' 7 ') delete() #删除指定条件的数据 改 models.Tb1.objects.filter (name=' 7 ') .update(性别=' 0 ')#将指定条件的数据更新,均支持* * kwargs obj=models.Tb1.objects.get (id=1) obj。c1=' 111 ' obj.save() #修改单条数据 >之前<强>查询简单操作强>
获取个数 models.Tb1.objects.filter (name=' 7 ') .count () 大于,小于 models.Tb1.objects.filter (id__gt=1) #获取id大于1的值 models.Tb1.objects.filter (id__gte=1) #获取id大于等于1的值 models.Tb1.objects.filter (id__lt=10) #获取id小于10的值 models.Tb1.objects.filter (id__lte=10) #获取id小于10的值 models.Tb1.objects。过滤器(id__lt=10, id__gt=1) #获取id大于1且小于10的值 在 models.Tb1.objects。过滤器(id__in=[33] 11日22日)#获取id等于11日,22日,33的数据 models.Tb1.objects。排除(id__in=[33] 11日22日)#不是 isnull Entry.objects.filter (pub_date__isnull=True) 包含 models.Tb1.objects.filter (name__contains=ven) models.Tb1.objects.filter (name__icontains=ven) # icontains大小写不敏感 models.Tb1.objects.exclude (name__icontains=ven) 范围 models.Tb1.objects。过滤器(id__range=[1, 2]) #范围bettwen和 其他类似 startswith、istartswith endswith iendswith, 命令 models.Tb1.objects.filter (name=' 7 ') .order_by (id) # asc models.Tb1.objects.filter (name=' 7 ') .order_by (id) # desc 集团——注释 从django.db。模型导入数,最小,最大,求和 models.Tb1.objects.filter (c1=1) . values (id) .annotate (c=Count (“num”)) 选择“app01_tb1”。”id”,计数(“app01_tb1”。“num”)作为“c”“app01_tb1”,“app01_tb1”。“c1=1 GROUP BY“app01_tb1”。“id” 限制,抵消 models.Tb1.objects.all () [20] regex正则匹配,iregex不区分大小写 Entry.objects.get (title__regex=r ' ^(您# 63;|)+ ') Entry.objects.get (title__iregex=r ' ^(您# 63;|)+ ') 日期 Entry.objects.filter (pub_date__date=datetime。日期(2005年,1,1)) Entry.objects.filter (pub_date__date__gt=datetime。日期(2005年,1,1)) 一年 Entry.objects.filter (pub_date__year=2005) Entry.objects.filter (pub_date__year__gte=2005) 月 Entry.objects.filter (pub_date__month=12) Entry.objects.filter (pub_date__month__gte=6) 一天 Entry.objects.filter (pub_date__day=3) Entry.objects.filter (pub_date__day__gte=3) week_day Entry.objects.filter (pub_date__week_day=2) Entry.objects.filter (pub_date__week_day__gte=2) 小时 Event.objects.filter (timestamp__hour=23) Event.objects.filter (time__hour=5) Event.objects.filter (timestamp__hour__gte=12) 一分钟 Event.objects.filter (timestamp__minute=29) Event.objects.filter (time__minute=46) Event.objects.filter (timestamp__minute__gte=29) 第二个 Event.objects.filter (timestamp__second=31) Event.objects.filter (time__second=2) Event.objects.filter (timestamp__second__gte=31)详解Django的模型查询操作与查询性能优化