Django--filter()-字段查找(双下划线的使用详解)


在了解django中的字段查找的同时,让我们先熟悉一下比较符:

大于--gt-(greater than)  小于--lt-(less than)  等于--eq-(equal)  大于等于--gte  小于等于--lte

>>> from queryset_demo.models import Blog,Author,Entry
>>> entry = Entry.objects.filter(pub_date__lte='2017-01-01')
>>> entry
<QuerySet [<Entry: python-2015>, <Entry: python-2014>, <Entry: python-4>]>
>>> [d.id for d in entry] # 返回数据的id
[5, 6, 7]

对应的sql语句为

mysql> select * from queryset_demo_entry
-> ;
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
| 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 |
| 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 |
| 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 |
| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
7 rows in set (0.00 sec) mysql> select * from queryset_demo_entry where pub_date<2017-01-01;
Empty set, 1 warning (0.00 sec) mysql> select * from queryset_demo_entry where pub_date<'2017-01-01';
+----+-------------+-----------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+-----------+------------+------------+------------+-------------+--------+---------+
| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
+----+-------------+-----------+------------+------------+------------+-------------+--------+---------+
3 rows in set (0.00 sec)

对应的sql语句

对外键ForeignKey的字段查找,同时也是一个特殊的地方就是可以通过单下划线

>>> entry
<QuerySet [<Entry: python>, <Entry: python-4>]>
>>> type(entry)
<class 'django.db.models.query.QuerySet'>
>>> [d.id for d in entry]
[1, 7]

sql

mysql> select * from queryset_demo_entry
-> ;
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
| 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 |
| 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 |
| 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 |
| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
7 rows in set (0.00 sec)

精确查找exact

>>> entry=Entry.objects.get(body_text__exact="This is a demo")
>>> entry.id
1 >>> entry=Entry.objects.get(body_text="This is a demo")
>>> entry.id
1

sql

mysql> select * from queryset_demo_entry
-> ;
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
| 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 |
| 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 |
| 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 |
| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
7 rows in set (0.00 sec) mysql> select * from queryset_demo_entry where body_text ='This is a demo';
+----+----------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+----------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
+----+----------+----------------+------------+------------+------------+-------------+--------+---------+
1 row in set (0.00 sec)

跨表查询

>>> Entry.objects.filter(blog__name='blog_3')
<QuerySet [<Entry: python>, <Entry: python-4>]>
>>> entry=Entry.objects.filter(blog__name='blog_3')
>>> [d.id for d in entry]
[1, 7]

sql

mysql> select * from queryset_demo_blog;
+----+-----------------+------------------------------+
| id | name | tagline |
+----+-----------------+------------------------------+
| 1 | change_new_name | All the latest Beatles news. |
| 2 | create_test | This is the wayof create |
| 3 | Cheddar Talk | One to Many test |
| 4 | blog_3 | this is a test |
+----+-----------------+------------------------------+
4 rows in set (0.00 sec) mysql> select * from queryset_demo_entry
-> ;
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
| 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 |
| 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 |
| 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 |
| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
7 rows in set (0.00 sec)

反向查询

通过blog表进行查询

它也可以倒退。要引用“反向”关系,只需使用模型的小写名称即可。

这个例子检索所有Blog具有至少一个对象Entry ,其headline包含'Lennon'

>>> from queryset_demo.models import *
>>> blog = Blog.objects.filter(entry__body_text__contains='dj')
>>> blog
<QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>> [d.id for d in blog]
[2, 3, 1]
>>>

跨越多值关系

使用下划线查找多值得关系

>>> blog = Blog.objects.filter(entry__body_text__contains='dj')
>>> blog
<QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>> [d.id for d in blog]
[2, 3, 1]
>>> blog = Blog.objects.filter(entry__body_text__contains='dj',entry__pub_date__year=2017)
>>> [d.id for d in blog]
[3, 1]
>>> blog = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017)
>>> blog_2 = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017)
>>> blog_2
<QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>> blog
<QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>>

英文:

  Suppose there is only one blog that had both entries containing “Lennon” and entries from 2008, but that none of the entries from 2008 contained “Lennon”. The first query would not return any blogs, but the   second query would return that one blog.

  In the second example, the first filter restricts the queryset to all those blogs linked to entries with “Lennon” in the headline. The second filter restricts the set of blogs further to those that are also linked to entries that were published in 2008. The entries selected by the second filter may or may not be the same as the entries in the first filter. We are filtering the Blog items with each filter statement, not the Entry items.

理解:

数据库进行数据的添加

mysql> mysql> select * from queryset_demo_entry;
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id |
+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
| 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 |
| 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 |
| 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 |
| 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 |

| 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 |
| 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 |
| 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 |
| 8 | demo | demo | 2017-06-16 | 2017-05-16 | 33 | 33 | 5 | 2 |
| 9 | demo2 | zzddxx | 2017-02-16 | 2017-02-04 | 44 | 33 | 22 | 3 |

+----+-------------+----------------+------------+------------+------------+-------------+--------+---------+
9 rows in set (0.00 sec) mysql> select * from queryset_demo_blog;
+----+-----------------+------------------------------+
| id | name | tagline |
+----+-----------------+------------------------------+
| 1 | change_new_name | All the latest Beatles news. |
| 2 | create_test | This is the wayof create |
| 3 | Cheddar Talk | One to Many test |
| 4 | blog_3 | this is a test |
+----+-----------------+------------------------------+
4 rows in set (0.01 sec)

shell进行测试

>>> blog = Blog.objects.filter(entry__body_text__contains='dj',entry__pub_date__year=2017)
>>> blog
<QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>>
>>>
>>> blog_2 = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017)
>>> blog_2
<QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: Cheddar Talk>, <Blog: change_new_name>]>
>>> [d.id for d in blog]
[3, 1]
>>> [d.id for d in blog_2]
[2, 3, 3, 1]

Django--filter()-字段查找(双下划线的使用详解)的更多相关文章

  1. Django 数据库查询集合(双下划线连表操作)

    Django是一款优秀的web框架,有着自己的ORM数据库模型.在项目中一直使用django数据库,写一篇文章专门记录一下数据库操作.略写django工程创建过程,详写查询过程.可以和sqlalche ...

  2. Django基础(5) ----基于双下划线的跨表查询,聚合查询,分组查询,F查询,Q查询

    一.基于双下划线的跨表查询 Django 还提供了一种直观而高效的方式在查询(lookups)中表示关联关系,它能自动确认 SQL JOIN 联系.要做跨关系查询,就使用两个下划线来链接模型(mode ...

  3. Django Mysql数据库-基于双下划线的跨表查询

    一.基于双下划线的跨表查询 Django 还提供了一种直观而高效的方式在查询(lookups)中表示关联关系,它能自动确认 SQL JOIN 联系.要做跨关系查询,就使用两个下划线来链接模型(mode ...

  4. django后台使用MySQL情况下的事务控制详解

    写在前面: 默认情况下django会把autocommit设置为“1”也就是说所针对数据库的每一次操作都会被做成“单独”的一个事务:这样的处理好处就在于它方便, 在编程的时候可以少写一些代码,比如我们 ...

  5. Django ORM字段类型 单表增删改查 万能的双下划线

    1.ORM三种模型 模型之间的三种关系:一对一,一对多,多对多. 一对一:实质就是在主外键(author_id就是foreign key)的关系基础上,给外键加了一个UNIQUE=True的属性: 一 ...

  6. django基础之day04,必知必会13条,双下划线查询,字段增删改查,对象的跨表查询,双下划线的跨表查询

    from django.test import TestCase # Create your tests here. import os import sys if __name__ == " ...

  7. Django框架之第六篇(模型层)--单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和参数、关系字段

    单表查询 补充一个知识点:在models.py建表是 create_time = models.DateField() 关键字参数: 1.auto_now:每次操作数据,都会自动刷新当前操作的时间 2 ...

  8. Django学习——Django测试环境搭建、单表查询关键字、神奇的双下划线查询(范围查询)、图书管理系统表设计、外键字段操作、跨表查询理论、基于对象的跨表查询、基于双下划线的跨表查询

    Django测试环境搭建 ps: 1.pycharm连接数据库都需要提前下载对应的驱动 2.自带的sqlite3对日期格式数据不敏感 如果后续业务需要使用日期辅助筛选数据那么不推荐使用sqlite3 ...

  9. django models的点查询/跨表查询/双下划线查询

    django models 在日常的编程中,我们需要建立数据库模型 而往往会用到表与表之间的关系,这就比单表取数据要复杂一些 在多表之间发生关系的情形下,我们如何利用models提供的API的特性获得 ...

随机推荐

  1. radhat6.6上安装oracle12c RAC (一)

    软件环境:VMware.redhat6.6.oracle12c(linuxx64_12201_database.zip).12cgrid(linuxx64_12201_grid_home.zip) 一 ...

  2. 基于OpenCV做“三维重建”(0)-- OpenCV3.2+VIZ6.3.0在vs2012下的编译和使用

    一.问题提出         ViZ对于显示3维的效果图来说,非常有帮助:我在使用OpenCV进行双目测距的过程中,有一些参数希望能够通过可视化的方法显示出来,所以参考了这方面相关的资料.做了一些实验 ...

  3. iperf详细使用方法

    Iperf  是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽, 具有多种参数和UDP特性.Iperf可以报告带宽,延迟抖动和数据包丢失. Iperf ...

  4. Python-王者荣耀自动刷金币+爬取英雄信息+图片

    前提:本文主要功能是 1.用python代刷王者荣耀金币 2.爬取英雄信息 3.爬取王者荣耀图片之类的. (全部免费附加源代码) 思路:第一个功能是在基于去年自动刷跳一跳python代码上面弄的,思路 ...

  5. ActiveReports 大数据分析报告:2019软件开发者现状

    “C++很不错,PHP是世界上最好的语言,所以我选Java …” 在全球软件开发者群体中,关于最优语言与最优框架的争论从未停止. 本次 ActiveReports 大数据分析报告,将借助权威数据,为您 ...

  6. [IoC容器Unity]第二回:Lifetime Managers生命周期

    1.引言 Unity的生命周期是注册的类型对象的生命周期,而Unity默认情况下会自动帮我们维护好这些对象的生命周期,我们也可以显示配置对象的生命周期,Unity将按照配置自动管理,非常方便,下面就介 ...

  7. vue的一些随记

    1.vue中在methods等中使用filters中的过滤器 this.$options.filters[filter](...args)

  8. mysql分库 分页查询

    Mysql海量数据分表分库如何列表分页? 1.现在使用ElasticSearch了.基于Lucene的解决方案 2.必须将mysql里的数据写入到类似hbase这样的分布式数据库,查询快.但分页.查询 ...

  9. mybatis 一对一关联 association 返回空值

    mybatis 一对一关联 association 返回空值 最近学习spring mvc + mybatis开发,看的书是<Spring MVC+Mybatis开发 从入门到精通>,在学 ...

  10. Python3爬虫系列:理论+实验+爬取妹子图实战

    Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...