Add 'exact' and 'iexact' match operators for QuerySets

This commit is contained in:
Flavio Amieiro
2010-05-26 20:24:57 -03:00
parent b23353e376
commit a2c78c9063
3 changed files with 30 additions and 3 deletions

View File

@@ -51,7 +51,7 @@ class StringField(BaseField):
if not isinstance(op, basestring):
return value
if op.lstrip('i') in ('startswith', 'endswith', 'contains'):
if op.lstrip('i') in ('startswith', 'endswith', 'contains', 'exact'):
flags = 0
if op.startswith('i'):
flags = re.IGNORECASE
@@ -62,6 +62,8 @@ class StringField(BaseField):
regex = r'^%s'
elif op == 'endswith':
regex = r'%s$'
elif op == 'exact':
regex = r'^%s$'
value = re.compile(regex % value, flags)
return value

View File

@@ -307,8 +307,9 @@ class QuerySet(object):
"""
operators = ['ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod',
'all', 'size', 'exists', 'near']
match_operators = ['contains', 'icontains', 'startswith',
'istartswith', 'endswith', 'iendswith']
match_operators = ['contains', 'icontains', 'startswith',
'istartswith', 'endswith', 'iendswith',
'exact', 'iexact']
mongo_query = {}
for key, value in query.items():