65.ORM查询条件:gte,gt,lte和lt的使用
1. gte: 代表的是大于等于,英文全称为:great than equal。举例:找到文章id大于等于3等文章,示例代码如下:
定义模型的示例代码如下:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'category'
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True)
def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)
class Meta:
db_table = 'article'
views.py文件中视图函数的示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
# gte:查找出文章id大于等于3的文章
articles = Article.objects.filter(id__gte=3)
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果:
<QuerySet [<Article: <(Article: id: 3,title: 钢铁是怎样炼成的, content: 你好)>>,
<Article: <(Article: id: 4,title: 中国吸引力, content: 精彩极了)>>]>
原生sql语句为:SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.id >= 3
2. gt:代表的是大于等于。举例查找id大于3的文章,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(id__gt=3)
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果:
<QuerySet [<Article: <(Article: id: 4,title: 中国吸引力, content: 精彩极了
)>>]>
原生sql语句:SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.id > 3
3.lte: 代表的是小于等于,举例查找id小于等于3的文章,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(id__lte=3)
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果:
<QuerySet [<Article: <(Article: id: 1,title: Hello, content: 你好)>>,
<Article: <(Article: id: 2,title: Hello World, content: 大家好)>>,
<Article: <(Article: id: 3,title: 钢铁是怎样炼成的, content: 你好)>>]>
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.id <= 3
4.lt: 代表的是小于。举例查找id小于3的文章。示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(id__lt=3)
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果:
<QuerySet [<Article: <(Article: id: 1,title: Hello, content: 你好)>>, <Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]>
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.id < 3
65.ORM查询条件:gte,gt,lte和lt的使用的更多相关文章
- ORM查询条件
模板: from django.db import models class Article(models.Model): title = models.CharField(max_length=20 ...
- 67.ORM查询条件:range的使用,使用make_aware将navie time 转换为aware time
模型的定义,models.py文件中示例代码如下: from django.db import models # 在定义模型的类时,一定要继承models.Model class Category(m ...
- 68.ORM查询条件:date,time,year,week_day等
1. date: 首先查看数据库中article表的信息,由表中的create_time字段可以看出时间为2020.2.5 打印出查询的结果: <QuerySet []>:但是查询的结果为 ...
- 69.ORM查询条件:isnull和regex的使用
首先查看数据库中的article表的数据: 定义模型的文件models.py中的示例代码如下: from django.db import models class Category(models.M ...
- 64.Python中ORM查询条件:in和关联模型
定义模型的models.py文件中示例代码如下: from django.db import models class Category(models.Model): name = models.Ch ...
- [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“
mongodb 查询条件 这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...
- mongodb中比较级查询条件:($lt $lte $gt $gte)(大于、小于)、查找条件
查询表中学生年级大于20,如下: db.getCollection('student').find({'age':{'$gt':'20'}}) $lt < (less than ) ...
- MongoDB小结14 - find【查询条件$lt $lte $gt $gte】
$lt $lte $gt $gte 以上四个分别表示为:< . <= . > . >= . 通常的做法是将他们组合起来,以便查找一个范围. 比如,查询年龄在18到25岁(含)的 ...
- django orm 的查询条件
Django的ORM查询操作: 查询数据库操作是一个非常重要的技术.在Django中,查询一般就是使用filter.exclude.get三个方法来实现,在调用这些方法的时候传递不同的查询条件来实现复 ...
随机推荐
- Vue-cli3与springboot项目整合打包
一.需求 使用前后端分离编写了个小程序,前端使用的是vue-cli3创建的项目,后端使用的是springboot创建的项目,部署的时候一起打包部署,本文对一些细节部分进行了说明. 二 ...
- linux下mysql允许远程连接
1. MySql安装教程 https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html 默认情况下mysq的 roo ...
- Tornado -- 7 - 查询结果
查询结果 查询结果总结: 条件查询 多表查询
- BSGS&ExBSGS
BSGS&ExBSGS 求解形如 \[a^x\equiv b\pmod p\] 的高次同余方程 BSGS 假装\(gcd(a,p)=1\). 设\(m=\lceil\sqrt p \rceil ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- 循环的N种写法
protype,json都算进去 先总结一下 伪数组的循环方式有,for,for-of 数组的循环方式有for,forEach,map,filter,find,some,every,reduce,fo ...
- 单选与多选与label
单选radio和多选checkbox是用name属性关联的 相同的name就相当于同一道题 <input type="radio" name="radio" ...
- NO6 alias-unalias命令,递归创建目录,如何取消覆盖提示
·如果需要更新补丁则执行:·yum update·yum install lrzsz telnet tree nmap nc -y·alias #查看系统现有的别名. 一.设置别名eg: ...
- Golang的进制转换实战案例
Golang的进制转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用进制概述 1>.进制概述 进制也就是进位制,是人们规定的一种进位方法.举个例子:二进制就 ...
- linux下安装redis,按照redis官网安装不成功需要提前安装c++环境(安装成功并可以测试)
这个安装是一种便捷的安装,没有几句,但是完全按照官网上的来没有安装成功,有前提条件的 打开linux root登录 然后在usr下面建文件夹redis,进入 在该文件加下,直接按照官网的指导进行安装即 ...