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的使用的更多相关文章

  1. ORM查询条件

    模板: from django.db import models class Article(models.Model): title = models.CharField(max_length=20 ...

  2. 67.ORM查询条件:range的使用,使用make_aware将navie time 转换为aware time

    模型的定义,models.py文件中示例代码如下: from django.db import models # 在定义模型的类时,一定要继承models.Model class Category(m ...

  3. 68.ORM查询条件:date,time,year,week_day等

    1. date: 首先查看数据库中article表的信息,由表中的create_time字段可以看出时间为2020.2.5 打印出查询的结果: <QuerySet []>:但是查询的结果为 ...

  4. 69.ORM查询条件:isnull和regex的使用

    首先查看数据库中的article表的数据: 定义模型的文件models.py中的示例代码如下: from django.db import models class Category(models.M ...

  5. 64.Python中ORM查询条件:in和关联模型

    定义模型的models.py文件中示例代码如下: from django.db import models class Category(models.Model): name = models.Ch ...

  6. [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“

    mongodb 查询条件   这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...

  7. mongodb中比较级查询条件:($lt $lte $gt $gte)(大于、小于)、查找条件

    查询表中学生年级大于20,如下: db.getCollection('student').find({'age':{'$gt':'20'}}) $lt    <   (less  than ) ...

  8. MongoDB小结14 - find【查询条件$lt $lte $gt $gte】

    $lt $lte $gt $gte 以上四个分别表示为:< . <= . > . >= . 通常的做法是将他们组合起来,以便查找一个范围. 比如,查询年龄在18到25岁(含)的 ...

  9. django orm 的查询条件

    Django的ORM查询操作: 查询数据库操作是一个非常重要的技术.在Django中,查询一般就是使用filter.exclude.get三个方法来实现,在调用这些方法的时候传递不同的查询条件来实现复 ...

随机推荐

  1. 「PA2014」Kuglarz

    传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...

  2. Python 网络编程之网络协议(未完待续)

    一:网络编程从两大架构开始 1.网络开发的两大架构 c/s 架构 : client  server B/S 架构 : Brower  server (1)bs 和 cs 架构之间的关系? (2)哪一种 ...

  3. 8张图片掌握JS原型链

  4. [noip2014]P2312 解方程

    P2312 解方程 其实这道题就是求一个1元n次方程在区间[1, m]上的整数解. 我们枚举[1, m]上的所有整数,带进多项式中看看结果是不是0即可. 这里有一个技巧就是秦九韶算法,请读者自行查看学 ...

  5. springboot官网->pom.xml文件

    springboot 2.1.6 pom.xml

  6. java#lambda相关之方法引用

    lambda在java中通常是()->{}这样的方式,来书写的.通常的lambda是四大函数型接口的一个“实现”. 如果我们要写的lambda已经有现成的实现了,那么就可以把现成的实现拿过来使用 ...

  7. Day6 - I - Sticks Problem POJ - 2452

    Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented b ...

  8. yii2.0 引入权限控制插件

    权限控制:"mdmsoft/yii2-admin": "~2.0" 教程:http://www.cnblogs.com/zyf-zhaoyafei/p/5825 ...

  9. 003.CI4框架CodeIgniter, 控制器Controllers的访问地址

    01.我们新建一个System文件夹,然后创建一个Login.php类,代码如下: <?php namespace App\Controllers\System; use App\Control ...

  10. kali linux终端快捷键设置

    kali里面是没有终端快捷键的,需要自己设置. 打开设置->设备->keyboard,拉到最下面点击加号来新建一个快捷键. 名称:打开终端 命令:gnome-terminal 快捷键:Ct ...