Skip to content

Instantly share code, notes, and snippets.

View brehaut's full-sized avatar

Andrew Brehaut brehaut

View GitHub Profile
@mattdw
mattdw / simple_search.py
Created April 19, 2012 23:24
A simple functional search query creator for Django.
from django.db.models import Q
from django.utils.text import smart_split
def get_or_query(query_string, fields):
return reduce(Q.__or__,
(Q(**{'%s__iregex' % field: r"[[:<:]]%s" % term.strip('\'"')})
for term in smart_split(query_string) if term not in ['and', 'or']
for field in fields),
Q())
@mattdw
mattdw / multiqueryset.py
Created March 8, 2012 02:17
Django MultiQuerySet (for easy reference)
class MultiQuerySet(object):
def __init__(self, *args, **kwargs):
self.querysets = args
self._count = None
def count(self):
if not self._count:
self._count = sum(len(qs) for qs in self.querysets)
return self._count