Add 'exact' and 'iexact' match operators for QuerySets
This commit is contained in:
parent
b23353e376
commit
a2c78c9063
@ -51,7 +51,7 @@ class StringField(BaseField):
|
|||||||
if not isinstance(op, basestring):
|
if not isinstance(op, basestring):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
if op.lstrip('i') in ('startswith', 'endswith', 'contains'):
|
if op.lstrip('i') in ('startswith', 'endswith', 'contains', 'exact'):
|
||||||
flags = 0
|
flags = 0
|
||||||
if op.startswith('i'):
|
if op.startswith('i'):
|
||||||
flags = re.IGNORECASE
|
flags = re.IGNORECASE
|
||||||
@ -62,6 +62,8 @@ class StringField(BaseField):
|
|||||||
regex = r'^%s'
|
regex = r'^%s'
|
||||||
elif op == 'endswith':
|
elif op == 'endswith':
|
||||||
regex = r'%s$'
|
regex = r'%s$'
|
||||||
|
elif op == 'exact':
|
||||||
|
regex = r'^%s$'
|
||||||
value = re.compile(regex % value, flags)
|
value = re.compile(regex % value, flags)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -307,8 +307,9 @@ class QuerySet(object):
|
|||||||
"""
|
"""
|
||||||
operators = ['ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod',
|
operators = ['ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod',
|
||||||
'all', 'size', 'exists', 'near']
|
'all', 'size', 'exists', 'near']
|
||||||
match_operators = ['contains', 'icontains', 'startswith',
|
match_operators = ['contains', 'icontains', 'startswith',
|
||||||
'istartswith', 'endswith', 'iendswith']
|
'istartswith', 'endswith', 'iendswith',
|
||||||
|
'exact', 'iexact']
|
||||||
|
|
||||||
mongo_query = {}
|
mongo_query = {}
|
||||||
for key, value in query.items():
|
for key, value in query.items():
|
||||||
|
@ -265,6 +265,30 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
obj = self.Person.objects(Q(name__iendswith='rossuM')).first()
|
obj = self.Person.objects(Q(name__iendswith='rossuM')).first()
|
||||||
self.assertEqual(obj, person)
|
self.assertEqual(obj, person)
|
||||||
|
|
||||||
|
# Test exact
|
||||||
|
obj = self.Person.objects(name__exact='Guido van Rossum').first()
|
||||||
|
self.assertEqual(obj, person)
|
||||||
|
obj = self.Person.objects(name__exact='Guido van rossum').first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
obj = self.Person.objects(name__exact='Guido van Rossu').first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
obj = self.Person.objects(Q(name__exact='Guido van Rossum')).first()
|
||||||
|
self.assertEqual(obj, person)
|
||||||
|
obj = self.Person.objects(Q(name__exact='Guido van rossum')).first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
obj = self.Person.objects(Q(name__exact='Guido van Rossu')).first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
|
||||||
|
# Test iexact
|
||||||
|
obj = self.Person.objects(name__iexact='gUIDO VAN rOSSUM').first()
|
||||||
|
self.assertEqual(obj, person)
|
||||||
|
obj = self.Person.objects(name__iexact='gUIDO VAN rOSSU').first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
obj = self.Person.objects(Q(name__iexact='gUIDO VAN rOSSUM')).first()
|
||||||
|
self.assertEqual(obj, person)
|
||||||
|
obj = self.Person.objects(Q(name__iexact='gUIDO VAN rOSSU')).first()
|
||||||
|
self.assertEqual(obj, None)
|
||||||
|
|
||||||
def test_filter_chaining(self):
|
def test_filter_chaining(self):
|
||||||
"""Ensure filters can be chained together.
|
"""Ensure filters can be chained together.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user