position support singular value #1565
This commit is contained in:
parent
3dcc9bc143
commit
433f10ef93
@ -304,10 +304,6 @@ def update(_doc_cls=None, **update):
|
||||
value = {match: value}
|
||||
|
||||
key = '.'.join(parts)
|
||||
position = None
|
||||
if parts[-1].isdigit() and isinstance(value, (list, tuple, set)):
|
||||
key = parts[0]
|
||||
position = int(parts[-1])
|
||||
|
||||
if not op:
|
||||
raise InvalidQueryError('Updates must supply an operation '
|
||||
@ -339,11 +335,18 @@ def update(_doc_cls=None, **update):
|
||||
value = {key: value}
|
||||
elif op == 'addToSet' and isinstance(value, list):
|
||||
value = {key: {'$each': value}}
|
||||
elif op == 'push' and isinstance(value, list):
|
||||
if position is not None:
|
||||
elif op == 'push':
|
||||
if parts[-1].isdigit() and op == 'push':
|
||||
key = parts[0]
|
||||
position = int(parts[-1])
|
||||
# position modifier must appear with each.
|
||||
if not isinstance(value, (set, tuple, list)):
|
||||
value = [value]
|
||||
value = {key: {'$each': value, '$position': position}}
|
||||
else:
|
||||
elif isinstance(value, list):
|
||||
value = {key: {'$each': value}}
|
||||
else:
|
||||
value = {key: value}
|
||||
else:
|
||||
value = {key: value}
|
||||
key = '$' + op
|
||||
|
@ -1925,6 +1925,11 @@ class QuerySetTest(unittest.TestCase):
|
||||
post.reload()
|
||||
self.assertEqual(post.tags, ['mongodb', 'python', 'java'])
|
||||
|
||||
#test push with singular value
|
||||
BlogPost.objects.filter(id=post.id).update(push__tags__0='scala')
|
||||
post.reload()
|
||||
self.assertEqual(post.tags, ['scala', 'mongodb', 'python', 'java'])
|
||||
|
||||
def test_update_push_and_pull_add_to_set(self):
|
||||
"""Ensure that the 'pull' update operation works correctly.
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user