(2)where条件查询
- 现在数据库中,手动增加几条数据,以便看到效果: mysql> insert into TestModel_aaa values(2,"lisi","22"); Query OK, 1 row affected (0.06 sec) mysql> insert into TestModel_aaa values(3,"wangwu","55"); Query OK, 1 row affected (0.08 sec) mysql> select * from TestModel_aaa; +----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | zhangsan | 34 | | 2 | lisi | 22 | | 3 | wangwu | 55 | +----+----------+-----+ 3 rows in set (0.00 sec) - 修改testdb.py # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response_id = "" response1 = "" response2 = "" list = Aaa.objects.filter(id=3) #相当于在 select 时添加where过滤 for i in list: response1 = i.id response2 += i.name + " " + i.age + "" response = response2 response_id = str(response1) return HttpResponse("<p>" + response_id + " " + response + "</p>")
访问
- 再次修改testdb.py
# -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response_id = "" response1 = "" response2 = "" list = Aaa.objects.filter(name="lisi") for i in list: response1 = i.id response2 += i.name + " " + i.age + "" response = response2 response_id = str(response1) return HttpResponse("<p>" + response_id + " " + response + "</p>")
访问
(3)获取单个对象
- 修改testdb.py # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): list = Aaa.objects.get(id=3) return HttpResponse("<p>" + str(list.id) + list.name + list.age +"</p>")
- 使用
get
获取的是单个对象,不是可迭代数据,所以可以直接取值,下面来访问一下
(4)限制返回的数据
- 注释 offset 1:表示跳过第1行,因为是根据下标,所以第一行数据行是0开始 limit 2:表示获取前两行 - 下面修改testdb.py文件 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response1 = "" list = Aaa.objects.order_by("name")[0:2] #限制返回的数据,相当于SQL中的offset 0 limit 2,也就是跳过0行,输出前1行 for i in list: response1 += i.name + " " response = response1 return HttpResponse("<p>" + response + "</p>")
访问,可以看到,显示的是前两行
再次修改进行访问
# -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response1 = "" list = Aaa.objects.order_by("name")[1:2] #相当于SQL中的offset 1 limit 2,也就是跳过第一行,输出前2行,因为跳过了一行,所以最终输出1行 for i in list: response1 += i.name + " " response = response1 return HttpResponse("<p>" + response + "</p>")
(5)数据排序
- 修改testdb.py文件 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response_id = "" response1 = "" response2 = "" list = Aaa.objects.order_by("id") #按照指定字段,从小到大排序 for i in list: response1 += str(i.id) response2 += i.name + " " + i.age + " " response = response2 response_id = response1 return HttpResponse("<p>" + response_id + " " + response + "</p>")
访问
- 上面是从小到大,下面是从大到小
- 修改testdb.py文件 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response_id = "" response1 = "" response2 = "" list = Aaa.objects.order_by("-id") #按照指定字段,从小到大排序,前面加 - for i in list: response1 += str(i.id) response2 += i.name + " " + i.age + " " response = response2 response_id = response1 return HttpResponse("<p>" + response_id + " " + response + "</p>")
- 访问
(6)混合查询
- 往数据库添加数据 mysql> insert into TestModel_aaa values(4,"lisi","22"); Query OK, 1 row affected (0.15 sec) mysql> select * from TestModel_aaa; +----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | zhangsan | 34 | | 2 | lisi | 22 | | 3 | wangwu | 55 | | 4 | lisi | 22 | +----+----------+-----+ 4 rows in set (0.00 sec) - 修改testdb.py文件 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Aaa def testdb(request): response = "" response_id = "" response1 = "" response2 = "" list = Aaa.objects.filter(name="lisi").order_by("id") #where查询name等于lisi的,然后通过id进行从小大到大排序 for i in list: response1 += str(i.id) response2 += i.name + " " + i.age + " " response = response2 response_id = response1 return HttpResponse("<p>" + response_id + " " + response + "</p>")
- 访问