Django(3)模型(二)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Django(3)模型(二)


(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>")

访问


b2d35521083b4698a392c6b137da2723.png


  • 再次修改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>")

访问


dc8b7cb2fdcb41b2bd1da5aacb023061.png


(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获取的是单个对象,不是可迭代数据,所以可以直接取值,下面来访问一下


c8deb4f6cb87446e8f2a9f5bc897dd19.png


(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>")

访问,可以看到,显示的是前两行

314b6bdbb3b04218bb198b1830f10cf4.png

再次修改进行访问

# -*- 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>")

image.png


(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>")


访问


image.png



  • 上面是从小到大,下面是从大到小


- 修改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>")


  • 访问

image.png

(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>")
  • 访问


image.png

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
SQL 数据库 索引
Django MTV - 模型层 - (专题)知识要点与实战案例
Django MTV - 模型层 - (专题)知识要点与实战案例
31 0
|
3月前
|
存储 安全 网络协议
Python 教程之 Django(9)对模型中的字段进行验证
Python 教程之 Django(9)对模型中的字段进行验证
30 0
Python 教程之 Django(9)对模型中的字段进行验证
|
3月前
|
API 数据库 Python
Python 教程之 Django(8)在 Django 管理界面中渲染模型
Python 教程之 Django(8)在 Django 管理界面中渲染模型
23 0
Python 教程之 Django(8)在 Django 管理界面中渲染模型
|
3月前
|
机器学习/深度学习 算法 TensorFlow
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
63 0
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
|
5月前
|
存储 JavaScript 前端开发
08 Django模型 - 定义模型
08 Django模型 - 定义模型
16 0
|
3月前
|
SQL 存储 API
Python 教程之 Django(7)Django 模型
Python 教程之 Django(7)Django 模型
48 1
Python 教程之 Django(7)Django 模型
|
4月前
|
SQL 存储 API
Python 教程之 Django(7)Django 模型
Python 教程之 Django(7)Django 模型
30 0
|
5月前
|
SQL 缓存 数据库
10 Django模型 - 模型查询
10 Django模型 - 模型查询
41 0
|
5月前
|
数据库 Python
09 Django模型 - 模型成员
09 Django模型 - 模型成员
21 0
|
5月前
|
SQL 关系型数据库 MySQL
07 Django模型 - ORM简介及MySQL数据库的使用
07 Django模型 - ORM简介及MySQL数据库的使用
36 0