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
filter(reduce(operator.or_, argument_list))
or this
filter(reduce(operator.and_, query_list))
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.
_ is usually a macro/function from gettext, it means the argument is a localized string. this is not limited to Django or Python. in fact gettext is originally a package for C programs, ported to many other languages over the years.
Django offers a QuerySet method called select_related() that allows you to retrieve related objects for one-to-many relationships. This translates to a single, more complex QuerySet, but you avoid additional queries when accessing the related objects. The select_related method is for ForeignKey and OneToOne fields.
values() Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With