这个运算符在Django`reduce(operator.and_,query_list)`中意味着什么 [英] what does this operator means in django `reduce(operator.and_, query_list)`

查看:188
本文介绍了这个运算符在Django`reduce(operator.and_,query_list)`中意味着什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此问题

使用args和kwargs动态构造Django过滤器查询

我无法获得此信息

filter(reduce(operator.or_,arguments_list))

或此

filter(reduce(operator.and_,query_list))

推荐答案

filter 是Django Model Manager的常规方法,因此无需执行任何操作

filter is a regular method of Django Model Manager, so there is nothing to explain.

reduce 是类似于以下代码的内置函数:

reduce is a built-in function similar to the code below:

def reduce(func, items):
    result = items.pop()
    for item in items:
        result = func(result, item)

    return result

其中 func 是用户定义的函数。

Where func is a user defined function.

operator.or _ 是python标准库函数,包装了运算符。类似于以下代码:

operator.or_ is a python standard library function that wraps the or operator. It is similar to this code:

def or_(a, b):
    return a | b

例如:

reduce(operator.or_, [False, False, True])

将返回 True

在您的示例上下文中,以及运算符已重载,因此它应返回一个由较小部分组成的新查询,这些查询均由运算符串联而成。

In your example context, the or and the and operators are overloaded and therefore it should return a new query combined of smaller parts all concatenated by or or and operator.

这篇关于这个运算符在Django`reduce(operator.and_,query_list)`中意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆