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}
|
value = {match: value}
|
||||||
|
|
||||||
key = '.'.join(parts)
|
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:
|
if not op:
|
||||||
raise InvalidQueryError('Updates must supply an operation '
|
raise InvalidQueryError('Updates must supply an operation '
|
||||||
@ -339,13 +335,20 @@ def update(_doc_cls=None, **update):
|
|||||||
value = {key: value}
|
value = {key: value}
|
||||||
elif op == 'addToSet' and isinstance(value, list):
|
elif op == 'addToSet' and isinstance(value, list):
|
||||||
value = {key: {'$each': value}}
|
value = {key: {'$each': value}}
|
||||||
elif op == 'push' and isinstance(value, list):
|
elif op == 'push':
|
||||||
if position is not None:
|
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}}
|
value = {key: {'$each': value, '$position': position}}
|
||||||
else:
|
elif isinstance(value, list):
|
||||||
value = {key: {'$each': value}}
|
value = {key: {'$each': value}}
|
||||||
else:
|
else:
|
||||||
value = {key: value}
|
value = {key: value}
|
||||||
|
else:
|
||||||
|
value = {key: value}
|
||||||
key = '$' + op
|
key = '$' + op
|
||||||
if key not in mongo_update:
|
if key not in mongo_update:
|
||||||
mongo_update[key] = value
|
mongo_update[key] = value
|
||||||
|
@ -1925,6 +1925,11 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
post.reload()
|
post.reload()
|
||||||
self.assertEqual(post.tags, ['mongodb', 'python', 'java'])
|
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):
|
def test_update_push_and_pull_add_to_set(self):
|
||||||
"""Ensure that the 'pull' update operation works correctly.
|
"""Ensure that the 'pull' update operation works correctly.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user