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三个方法来实现,在调用这些方法的时候传递不同的查询条件来实现复 ...
随机推荐
- 5.5 Nginx 负载均衡
ip_hash 语法:ip_hash 默认值:none 使用字段:upstream 这个指令将基于客户端连接的IP地址来分发请求.哈希的关键字是客户端的C类网络地址,这个功能将保证这个客户端请求总是被 ...
- 2-10 就业课(2.0)-oozie:11、hadoop的federation(联邦机制,了解一下)
==================================================== Hadoop Federation 背景概述 单NameNode的架构使得HDFS在集群扩展性 ...
- esxi 版本升级命令
先把zip文件通过XShell或者WinSCP上传到esxi服务器上面去,然后执行以下命令,完成升级并重启就可以了.
- springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)
总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧. 首先,创建多个s ...
- phpstrom+win10拼音输入法不跟随情况
PHPSTORM拼中文时悬浮框一直在右下角,真是逼死强迫症的操作! 最好解决方案: 下载讯飞输入法,虽然有点不习惯,用着用着就行了 等待官方修复着bug吧; 网上说的直接下载jre64覆盖原来的版本也 ...
- 07.Delphi接口的生命周期
在Delphi的接口中,是不需要释放的,调用完之后,接口的生命周期就结束了,如下面的例子 unit mtReaper; interface type // 定义一个接口 IBase = interfa ...
- python SSTI tornado render模板注入
原理tornado render是python中的一个渲染函数,也就是一种模板,通过调用的参数不同,生成不同的网页,如果用户对render内容可控,不仅可以注入XSS代码,而且还可以通过{{}}进行传 ...
- 十六、React 渲染数据注意事项、以及react-router4.x中使用js跳转路由(登录成功自动跳转首页)
一.React加载数据流程回顾 先看上一节的产品详情代码:https://blog.csdn.net/u010132177/article/details/103184176 [Pcontent.js ...
- maven项目使用mybatis+mysql
1.添加依赖,在pom.xml中添加 <!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId ...
- java基础源码 (6)--ArrayListt类
原作出处:https://www.cnblogs.com/leesf456/p/5308358.html 简介: 1.ArrayList是一个数组队列,相当于动态数组.与java中的数组相比,它的容量 ...