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
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…