Django使用dict参数过滤OR条件

Mau*_*cio 7 python django

我在我的Django应用程序上有一个函数,我执行一些Queryset操作并将其结果设置为Memcache.由于它是一种功能,因此必须具有一般用途.所以为了使它可重用,我传递一个dict作为参数filterexclude动作.这是功能:

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
Run Code Online (Sandbox Code Playgroud)

如果我用一个简单的dict过滤查询集,它工作得很好{'title': 'foo', 'name': 'bar'}.然而,并非总是如此.我需要使用该django.db.models.Q实用程序执行过滤器,以进行需要OR条件的更复杂的查询.

那么,我如何将这些参数作为字典传递给过滤器.这有什么办法吗?

Mos*_*oye 9

您可以将字典重组为单个键值字典列表,并dictQ表达式内的每个字典中使用解包,如下所示:

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()])))
Run Code Online (Sandbox Code Playgroud)

reduceon or_连接QOR 上的表达式.

你也可以使用,你有一台发电机表达listdictS:

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


Ian*_*ice 5

您可以使用按位运算|符。

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'))
Run Code Online (Sandbox Code Playgroud)

考虑到这一点,您可能希望将存储在其中的所有查询加载my_filterQ对象中。然后,您可以通过与按位相同的方法加入所有非 OR 查询&:my_filter &= ...