Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有或条件的Django筛选器使用dict参数_Python_Django - Fatal编程技术网

Python 带有或条件的Django筛选器使用dict参数

Python 带有或条件的Django筛选器使用dict参数,python,django,Python,Django,我在Django应用程序上有一个函数,我在其中执行一些Queryset操作,并将其结果设置为Memcache。因为它是一个函数,所以它必须是通用的。因此,为了使其可重用,我将dict作为参数传递给filter和exclude操作。这就是功能: def cached_query(key, model, my_filter=None, exclude=None, order_by=None, sliced=50): """ :param key: string used as key

我在Django应用程序上有一个函数,我在其中执行一些Queryset操作,并将其结果设置为Memcache。因为它是一个函数,所以它必须是通用的。因此,为了使其可重用,我将dict作为参数传递给
filter
exclude
操作。这就是功能:

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
如果我用一个简单的dict(如
{'title':'foo','name':'bar}
过滤查询集,效果会非常好。然而,情况并非总是如此。我需要使用
django.db.models.Q
实用程序对需要
条件的更复杂查询执行筛选


那么,如何将这些参数作为字典传递给过滤器呢。有什么方法可以做到这一点吗?

您可以将您的字典重新构造为一个单键值字典列表,并在
Q
表达式中的每个
dict
上使用解包,如下所示:

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()])))
reduce
on
Q
表达式连接到or上

您还可以使用生成器表达式,其中包含
dict
s的
list

q = model.objects.filter(reduce(operator.or_, 
                                (Q(**d) for d in (dict([i]) for i in my_filter.items()))))

您可以使用按位
|
运算符

my_filter = Q()

# Or the Q object with the ones remaining in the list
my_or_filters = {'some_field__gte':3.5, 'another_field':'Dick Perch'}

for item in my_or_filters:
    my_filter |= Q(**{item:my_or_filters[item]})

model.objects.filter(my_filter)
# unpacks to model.objects.filter(Q(some_field__gte=3.5) | Q(another_field='Dick Perch'))
记住这一点,您可能希望将存储在
my_filter
中的所有查询加载到
Q
对象中。然后,您可以通过上面的相同方法,通过按位
连接所有非OR查询:

根据@Moses Koledoye的回答,我可以解决这个问题。这就是我的函数现在的样子:

 cached_query(key, model, my_filter=None, or_filter={}, 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 or_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:
        result = model.objects.all()
        if my_filter:
            result = model.objects.filter(**my_filter)
        if or_filter:
            reduced_filter = reduce(operator.or_, (Q(**d) for d in [dict([i]) for i in or_filter.items()]))
            result = result.filter(reduced_filter)
        if exclude:
            result = result.exclude(**exclude)
        if order_by:
            result = result.order_by(*order_by)
        result = result[:sliced]
        cache.set(key, result, cache_timeout)
    return result

哎呀,在引号里有一个项目。如上所述。