class X(object):
def __init__(self, field):
self.field = field
def _eval(self, other, lookup):
from django.db.models import Q
return Q(**{ '%s__%s' % ( self.field, lookup ) : other })
def __eq__(self, other):
return self._eval(True, 'isnull') if other is None else self._eval(other, 'exact')
def __ne__(self, other):
return ~ ( self == other )
def __lt__(self, other):
return self._eval(other, 'lt')
def __le__(self, other):
return self._eval(other, 'lte')
def __gt__(self, other):
return self._eval(other, 'gt')
def __ge__(self, other):
return self._eval(other, 'gte')
def __mod__(self, other):
return self._eval(other, 'icontains')
def __lshift__(self, other):
return self._eval(other, 'istartswith')
def __rshift__(self, other):
return self._eval(other, 'iendswith')
def __or__(self, other):
return self._eval(other, 'in')
Comments
great snippet!
#