what does this operator means in django `reduce(operator.and_, query_list)`

前端 未结 2 2020
孤城傲影
孤城傲影 2021-02-05 15:10

I am reading this questions

Constructing Django filter queries dynamically with args and kwargs

I am not able to get what does this operator do

fi

2条回答
  •  终归单人心
    2021-02-05 15:47

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

    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
    

    Where func is a user defined function.

    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
    

    For example:

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

    Will return 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.

提交回复
热议问题