使用dict参数的OR条件的Django过滤器 [英] Django filter with OR condition using dict argument
问题描述
我在Django应用中有一个函数,可以执行一些Queryset操作并将其结果设置为Memcache。由于它是一种功能,因此必须具有通用性。因此,为了使其可重用,我将dict作为 filter
和 exclude
操作的参数传递。这是函数:
I have a function on my Django app where I perform some Queryset actions and set it's result to Memcache. Since it is a function it has to be of general usage. So in order to make it reusable I pass a dict as parameter for filter
and exclude
actions. This is the function:
def cached_query(key, model, my_filter=None, exclude=None, order_by=None, sliced=50):
"""
:param key: string used as key reference to store on Memcached
:param model: model reference on which 'filter' will be called
:param my_filter: dictionary containing the filter parameters (eg.: {'title': 'foo', 'category': 'bar'}
:param sliced: integer limit of results from the query. The lower the better, since for some reason Django Memcached
won't store thousands of entries in memory
:param exclude: dictionary containing the exclude parameters (eg.: {'title': 'foo', 'category': 'bar'}
:param order_by: tuple containing the list of fields upon which the model will be ordered.
:return: list of models. Not a QuerySet, since it was sliced.
"""
result = cache.get(key, None)
if not result:
if my_filter:
result = model.objects.filter(**my_filter)
if exclude:
result = result.exclude(**exclude)
if order_by:
result = result.order_by(*order_by)
else:
result = model.objects.all()
result = result[:sliced]
cache.set(key, result, cache_timeout)
return result
如果我使用这样的简单字典过滤查询集,效果很好{'title':'foo','name':'bar'}
。但是,情况并非总是如此。对于需要 OR
条件的更复杂的查询,我需要使用 django.db.models.Q
实用工具执行过滤器。
It works pretty fine if I filter the queryset with a simple dict like {'title': 'foo', 'name': 'bar'}
. However that won't be always the case. I need to perform filters using the django.db.models.Q
utility for more complex queries which require OR
condition.
因此,如何将这些参数作为字典传递给过滤器。
So, how can I pass these parameters as a dictionary on a filter. Is there any approach for this?
推荐答案
您可以将字典重组为单个键值字典列表,并在其中使用解压缩功能 Q
表达式中的每个 dict
像这样:
You can restructure your dictionary into a list of single key-value dictionaries and use unpacking on each dict
inside the Q
expression like so:
from functools import reduce
import operator
from django.db.models import Q
# your dict is my_filter
q = model.objects.filter(reduce(operator.or_,
(Q(**d) for d in [dict([i]) for i in my_filter.items()])))
减少
或在OR上加入 Q
表达式。
您还可以使用生成器表达式,其中您有列表的
列表
:
You could also use a generator expression where you have the list
of dict
s:
q = model.objects.filter(reduce(operator.or_,
(Q(**d) for d in (dict([i]) for i in my_filter.items()))))
这篇关于使用dict参数的OR条件的Django过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!