Django--filter()-字段查找(双下划线的使用详解)
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()-字段查找(双下划线的使用详解)的更多相关文章
- Django 数据库查询集合(双下划线连表操作)
Django是一款优秀的web框架,有着自己的ORM数据库模型.在项目中一直使用django数据库,写一篇文章专门记录一下数据库操作.略写django工程创建过程,详写查询过程.可以和sqlalche ...
- Django基础(5) ----基于双下划线的跨表查询,聚合查询,分组查询,F查询,Q查询
一.基于双下划线的跨表查询 Django 还提供了一种直观而高效的方式在查询(lookups)中表示关联关系,它能自动确认 SQL JOIN 联系.要做跨关系查询,就使用两个下划线来链接模型(mode ...
- Django Mysql数据库-基于双下划线的跨表查询
一.基于双下划线的跨表查询 Django 还提供了一种直观而高效的方式在查询(lookups)中表示关联关系,它能自动确认 SQL JOIN 联系.要做跨关系查询,就使用两个下划线来链接模型(mode ...
- django后台使用MySQL情况下的事务控制详解
写在前面: 默认情况下django会把autocommit设置为“1”也就是说所针对数据库的每一次操作都会被做成“单独”的一个事务:这样的处理好处就在于它方便, 在编程的时候可以少写一些代码,比如我们 ...
- Django ORM字段类型 单表增删改查 万能的双下划线
1.ORM三种模型 模型之间的三种关系:一对一,一对多,多对多. 一对一:实质就是在主外键(author_id就是foreign key)的关系基础上,给外键加了一个UNIQUE=True的属性: 一 ...
- django基础之day04,必知必会13条,双下划线查询,字段增删改查,对象的跨表查询,双下划线的跨表查询
from django.test import TestCase # Create your tests here. import os import sys if __name__ == " ...
- Django框架之第六篇(模型层)--单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和参数、关系字段
单表查询 补充一个知识点:在models.py建表是 create_time = models.DateField() 关键字参数: 1.auto_now:每次操作数据,都会自动刷新当前操作的时间 2 ...
- Django学习——Django测试环境搭建、单表查询关键字、神奇的双下划线查询(范围查询)、图书管理系统表设计、外键字段操作、跨表查询理论、基于对象的跨表查询、基于双下划线的跨表查询
Django测试环境搭建 ps: 1.pycharm连接数据库都需要提前下载对应的驱动 2.自带的sqlite3对日期格式数据不敏感 如果后续业务需要使用日期辅助筛选数据那么不推荐使用sqlite3 ...
- django models的点查询/跨表查询/双下划线查询
django models 在日常的编程中,我们需要建立数据库模型 而往往会用到表与表之间的关系,这就比单表取数据要复杂一些 在多表之间发生关系的情形下,我们如何利用models提供的API的特性获得 ...
随机推荐
- qt 串口
if(ui->connectSerialBtn->text() == tr("打开串口")) { //ui->showSerialInfo->setEnab ...
- LintCode 1.A+B的问题
LintCode 1.A+B的问题 描述 给出两个整数 a 和 b , 求他们的和. 答案 public class Solution { /** * @param a: An integer * @ ...
- ProtoType原型和__Proto__原型链的详解
var arr = new Array(12,13,14,15,16,17,18); var arr2 = new Array(12,13); arr.sun=function(){ //用原型加Ar ...
- 8位、16位、32位单片机中的“XX位”指什么?
32位单片机的32位是指单片机的“字长”,也就是一次运算中参与运算的数据长度,这个位是指二进制位. 如果总线宽度与CPU一次处理的数据宽度相同,则这个宽度就是所说的单片机位数. 如果总线宽度与CPU一 ...
- C#socket编程之实现一个简单的TCP通信
TCP(TransmissionControl Protocol)传输控制协议. 是一种可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TC ...
- js斐波那契数列
斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...... 这个数列从第3项开始,每一项都等于前两项之和. 1.递归算法: function ...
- rsyslog+loganalyzer配置
1.loganalyzer服务器搭建[root@localhost ~]# getenforce #查看selinux是否关闭[root@localhost ~]# setenforce 0 #临时关 ...
- 20190320_head first pyhton学习笔记之构建发布
1.把代码nester.py放入文件夹nester中,在文件夹中再新建一个setup.py文件,文件内容如下: from distutils.core import setup setup( name ...
- idea中xml打开方式变成file,代码变成灰色
耗费了很久啊.... 新建文件的时候没有输入后缀.xml.自此无论复制修改还是新建都是文件的格式,其他名字倒是可以,但是我不能修改名字. 解决办法: OK了:
- max of 直线划平面
在一个无限延伸平面上有一个圆和n条直线,这些直线中每一条都在一个圆内,并且同其他所有的直线相交,假设没有3条直线相交于一点,试问这些直线最多将圆分成多少区域. Input 第一行包含一个整数T,(0& ...