在Django中有相当多的操作是通过双下划线与动作连接起来使用,为了以后更加方便的查找和使用,现在总结以下Django中基本的双下划线操作

  比较符:大于--gt  小于--lt 等于--eq  大于等于--gte 小于等于--lte

  

models.Example.objects.filter(id__gt=1)              # 获取id大于1的值
models.Example.objects.filter(id__gte=1) # 获取id大于等于1的值
models.Example.objects.filter(id__lt=10) # 获取id小于10的值
models.Example.objects.filter(id__lte=10) # 获取id小于10的值
models.Example.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值

  范围操作符:

包含-- in   在范围内--range

models.Example.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据
models.Example.objects.exclude(id__in=[11, 22, 33]) # not in 实际上是exclude的函数生效

包括--contain

models.Example.objects.filter(name__contains="ven")
models.Example.objects.filter(name__icontains="ven") # icontains大小写不敏感
models.Example.objects.exclude(name__icontains="ven")

在范围内--range

models.Example.objects.filter(id__range=[1, 2])   # 范围bettwen 1 and 2

  匹配操作符

为空--isnull

Entry.objects.filter(pub_date__isnull=True)

字符匹配:startswith,istartswith, endswith, iendswith,  ‘i代表大小写不敏感’

  类操作符:

对某一类进行排序--order by

models.Example.objects.filter(name='seven').order_by('id')    # asc 升序
models.Example.objects.filter(name='seven').order_by('-id') # desc降序

对某一类进行归类--group by

from django.db.models import Count, Min, Max, Sum
models.Example.objects.filter(c1=1).values('id').annotate(c=Count('num'))
SELECT "app01_Example"."id", COUNT("app01_Example"."num") AS "c" FROM "app01_Example" WHERE "app01_Example"."c1" = 1 GROUP BY "app01_Example"."id"

正则匹配 regex iregex(不区分大小写)

Entry.objects.get(title__regex=r'^(An?|The) +')
Entry.objects.get(title__iregex=r'^(an?|the) +')

日期相关 date year month day week_day hour minute second

Entry.objects.filter(pub_date__date=datetime.date(2017, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2017, 1, 1)) Entry.objects.filter(pub_date__year=2017)
Entry.objects.filter(pub_date__year__gte=2017) Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6) Entry.objects.filter(pub_date__day=3)
Entry.objects.filter(pub_date__day__gte=3) Entry.objects.filter(pub_date__week_day=2)
Entry.objects.filter(pub_date__week_day__gte=2) Event.objects.filter(timestamp__hour=23)
Event.objects.filter(time__hour=5)
Event.objects.filter(timestamp__hour__gte=12) Event.objects.filter(timestamp__minute=29)
Event.objects.filter(time__minute=46)
Event.objects.filter(timestamp__minute__gte=29) Event.objects.filter(timestamp__second=31)
Event.objects.filter(time__second=2)
Event.objects.filter(timestamp__second__gte=31)

 

Django中下划线的用法介绍(一)的更多相关文章

  1. Scala 中下划线的用法

    1.存在性类型:Existential types def foo(l: List[Option[_]]) = ... 2.高阶类型参数:Higher kinded type parametersca ...

  2. Scala 中下划线的用途

    转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...

  3. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  4. 转载:浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  5. Python中下划线的使用方法

    本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...

  6. MyEclipse相关用法介绍

    MyEclipse相关用法介绍 ================================================================================ 编辑: ...

  7. Python中下划线---完全解读(转)

      Python中下划线---完全解读 Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx ...

  8. Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍

    大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...

  9. 好压(HaoZip)的命令行模式用法介绍

    好压压缩软件,又叫“2345好压”,是一款国产的优秀压缩软件,目前是免费的,据官网介绍,该软件永久免费.官网地址:http://haozip.2345.com/ 本文主要对该软件的命令行模式用法进行介 ...

随机推荐

  1. [NOIP]2016天天爱跑步

    [NOIP]2016天天爱跑步 标签: LCA 树上差分 NOIP Description 小C同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是 ...

  2. 【Unity3D技术文档翻译】第1.2篇 为打包 AssetBundles 准备资产

    本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Development]→[AssetBundles]→[Preparing Assets f ...

  3. Mysql(二):库操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...

  4. 数据库中字段类型对应的C#中的数据类型(转载)

    数据库中字段类型对应C#中的数据类型: 数据库                 C#程序 int int32 text string bigint int64 binary System.Byte[] ...

  5. Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer

    在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...

  6. Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv

    Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/  mave ...

  7. 浏览器的 bfcache 特性

    一.bfcache 基本概念 现代浏览器在根据历史记录进行前进/后退操作时,会启用缓存机制,名为"bfcache"(back-forward cache,往返缓存),它使页面导航非 ...

  8. JAVA在不确定具体 Annotation 类型时,获得注解参数

       package com.lzw.demo; @SpringBootApplication public class DemoApplication { public static void ma ...

  9. XAF_GS_02_创建第一个XAF项目

    上一节我们讲解了如何安装XAF环境,这次我们要开始创建一个自己的XAF项目. Setp 1 第一步打开你的Visual Studio,新建项目,如下图所示,选择DevExpress XAF,选择好你的 ...

  10. python环境jieba分词的安装

    我的python环境是Anaconda3安装的,由于项目需要用到分词,使用jieba分词库,在此总结一下安装方法. 安装说明======= 代码对 Python 2/3 均兼容 * 全自动安装:`ea ...