django之Q
def _add_q(self, q_object, used_aliases, branch_negated=False,
current_negated=False, allow_joins=True, split_subq=True):
"""
Adds a Q-object to the current filter.
"""
connector = q_object.connector
current_negated = current_negated ^ q_object.negated#或,判断要不要取反
branch_negated = branch_negated or q_object.negated
target_clause = self.where_class(connector=connector,
negated=q_object.negated)
joinpromoter = JoinPromoter(q_object.connector, len(q_object.children), current_negated)
for child in q_object.children:#promotion促销,提升
if isinstance(child, Node):
child_clause, needed_inner = self._add_q(
child, used_aliases, branch_negated,
current_negated, allow_joins, split_subq)
joinpromoter.add_votes(needed_inner)
else:
child_clause, needed_inner = self.build_filter(
child, can_reuse=used_aliases, branch_negated=branch_negated,
current_negated=current_negated, connector=connector,
allow_joins=allow_joins, split_subq=split_subq,
)
joinpromoter.add_votes(needed_inner)
if child_clause:
target_clause.add(child_clause, connector)
needed_inner = joinpromoter.update_join_types(self)
return target_clause, needed_inner
class Node(object):
"""
A single internal node in the tree graph. A Node should be viewed as a
connection (the root) with the children being either leaf nodes or other
Node instances.
"""
# Standard connector type. Clients usually won't use this at all and
# subclasses will usually override the value.
default = 'DEFAULT' def __init__(self, children=None, connector=None, negated=False):
"""
Constructs a new Node. If no connector is given, the default will be
used.
"""
self.children = children[:] if children else []
self.connector = connector or self.default
self.negated = negated # We need this because of django.db.models.query_utils.Q. Q. __init__() is
# problematic, but it is a natural Node subclass in all other respects.
@classmethod
def _new_instance(cls, children=None, connector=None, negated=False):
"""
This is called to create a new instance of this class when we need new
Nodes (or subclasses) in the internal code in this class. Normally, it
just shadows __init__(). However, subclasses with an __init__ signature
that is not an extension of Node.__init__ might need to implement this
method to allow a Node to create a new instance of them (if they have
any extra setting up to do).
"""
obj = Node(children, connector, negated)
obj.__class__ = cls
return obj def __str__(self):
if self.negated:
return '(NOT (%s: %s))' % (self.connector, ', '.join(str(c) for c
in self.children))
return '(%s: %s)' % (self.connector, ', '.join(str(c) for c in
self.children)) def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self) def __deepcopy__(self, memodict):
"""
Utility method used by copy.deepcopy().
"""
obj = Node(connector=self.connector, negated=self.negated)
obj.__class__ = self.__class__
obj.children = copy.deepcopy(self.children, memodict)
return obj def __len__(self):
"""
The size of a node if the number of children it has.
"""
return len(self.children) def __bool__(self):
"""
For truth value testing.
"""
return bool(self.children) def __nonzero__(self): # Python 2 compatibility
return type(self).__bool__(self) def __contains__(self, other):
"""
Returns True is 'other' is a direct child of this instance.
"""
return other in self.children def add(self, data, conn_type, squash=True):
"""
Combines this tree and the data represented by data using the
connector conn_type. The combine is done by squashing the node other
away if possible. This tree (self) will never be pushed to a child node of the
combined tree, nor will the connector or negated properties change. The function returns a node which can be used in place of data
regardless if the node other got squashed or not. If `squash` is False the data is prepared and added as a child to
this tree without further logic.
"""
if data in self.children:#节点在孩子中,直接返回
return data
if not squash:#不挤进,追加到孩子中
self.children.append(data)
return data
if self.connector == conn_type:#连接子相同
# We can reuse self.children to append or squash the node other.
if (isinstance(data, Node) and not data.negated
and (data.connector == conn_type or len(data) == 1)):
# We can squash the other node's children directly into this
# node. We are just doing (AB)(CD) == (ABCD) here, with the
# addition that if the length of the other node is 1 the
# connector doesn't matter. However, for the len(self) == 1
# case we don't want to do the squashing, as it would alter
# self.connector.
self.children.extend(data.children)#加入孩子
return self
else:
# We could use perhaps additional logic here to see if some
# children could be used for pushdown here.
self.children.append(data)
return data
else:#连接字不相同,新创建一个node
obj = self._new_instance(self.children, self.connector,
self.negated)
self.connector = conn_type
self.children = [obj, data]
return data def negate(self):
"""
Negate the sense of the root connector.
"""
self.negated = not self.negated
django之Q的更多相关文章
- Django中Q查询及Q()对象
问题 一般我们在Django程序中查询数据库操作都是在QuerySet里进行进行,例如下面代码: >>> q1 = Entry.objects.filter(headline__st ...
- 浅谈Django的Q查询以及AngularJS的Datatables分页插件
使用Q查询,首先要导入Q模块: from django.db.models import Q 可以组合使用&,|操作符用于多个Q的对象,产生一个新的Q对象,Q对象也可以用~操作符放在前面表示否 ...
- Django中Q搜索的简单应用
本节涉及: 1.Q搜索在前后端的设计 2.Django中Queryset对象的序列化(由后端扔给前端的数据必然会经过序列化) 3.前端动态地构造表格以便显示(动态创建DOM对象) 思路: 用户通过前端 ...
- Django orm Q查询补充
Q的简单用法 from django.db.models import Q q = Q() q.children.append(("username", "lyj&quo ...
- Django Q对象
使用Q 对象进行复杂的查询¶ filter() 等方法中的关键字参数查询都是一起进行“AND” 的. 如果你需要执行更复杂的查询(例如OR 语句),你可以使用Q 对象. Q 对象 (django.db ...
- python 之 Django 小案例
一, F Q # F 使用查询条件的值 # # from django.db.models import F # models.Tb1.objects.update(num=F('num')+1) ...
- Python之路【第二十一篇】Django ORM详解
ORM回顾 关系对象映射(Object Relational Mapping,简称ORM). django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表. 对于ORM框 ...
- django manytomany
转载:http://my.oschina.net/u/572994/blog/105280 例如有如下模型 models.py ? 1 2 3 4 5 6 7 from django.db impor ...
- django的前后的结合,search搜索功能案例
利用django的Q()功能可以很好的展开搜索功能 假设我要做个这样的搜索功能
随机推荐
- JFrame关闭程序就退出的设置
要是实现JFrame 关闭后程就退出或者停止可以调用setDefaultCloseOperation() 方法. 入下图程序 package ltb6w; import javax.swing.*; ...
- <亲测>CentOS7中使用yum安装Nginx的方法
CentOS7中使用yum安装Nginx的方法 最近无意间发现Nginx官方提供了Yum源.因此写个文章记录下. 1.添加源 默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了 ...
- P2064进制转换
题目:https://www.luogu.org/problemnew/show/P2084 既然这道题放在字符串类型里,那么这里的N肯定得用字符数组来储存(这样也方便输出). 那么我们不妨定义一个字 ...
- java的应用包的方法,及调用类里面函数的原理
selenium官网下载的selenium包 包导入eclipse 见:https://www.cnblogs.com/kaibindirver/p/10674604.html 代码
- grafana 指标视图嵌入到其他html网页
我们开发了一套管理平台用来监控整个系统环境的运行情况,但是在指标信息这块不想重新开发,而想直接拿grafana来用,刚开始的时候我们的管理平台和grafana是完全独立的,只能从我们平台跳转到graf ...
- 关于ros里ppp拨号隧道比如pptp,l2tp,sstp等等,造成多条路由,ospf的时候需要汇总为一条宣告的解决方案
官方解决方案: https://wiki.mikrotik.com/wiki/OSPF_and_PPPoE_Setup 实际解决步骤: So to get rid of /32 routes * on ...
- KPPW2.5 漏洞利用--SQL注入
KPPW2.5 漏洞利用--SQL注入 SQL注入--布尔型盲注 环境搭建 1,集成环境简单方便,如wamp,phpstudy.... 2,KPPW v2.2源码一份(文末有分享)放到WWW目录下面 ...
- javaScript read blob
http://www.cnblogs.com/wangfajing/p/7202139.html?utm_source=itdadao&utm_medium=referral
- Epic Games工程师分享:如何在移动平台上做UE4的UI优化?
转自:https://blog.csdn.net/debugconsole/article/details/79281290 随着技术的不断升级,高性能的引擎逐渐受到越来越多研发商的青睐,UE4就是其 ...
- (转)App.Config详解及读写操作
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...