zoukankan      html  css  js  c++  java
  • 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

    始于才华,忠于颜值;每件事情在成功之前,看起来都是天方夜谭。一无所有,就是无所不能。
  • 相关阅读:
    《信息安全系统设计基础》实验中的问题总结
    20145233计算机病毒实践7之动态分析3
    20145233计算机病毒实践3之静态分析3
    20145233计算机病毒实践5之动静态分析
    20145233计算机病毒实践2之静态分析
    20145233计算机病毒实践1之静态工具介绍
    20145233计算机病毒实践十
    20145233计算机病毒实践九之IDA的使用
    20145233韩昊辰小组 课程设计中期检查
    20145233《网络对抗》Exp9 Web安全基础实践
  • 原文地址:https://www.cnblogs.com/guyan-2020/p/12262494.html
Copyright © 2011-2022 走看看