Updates with no operator should default to $set Fix #667

This commit is contained in:
Bruno Rocha 2014-07-02 14:39:29 -03:00
parent 21818e71f5
commit 99e943c365
3 changed files with 14 additions and 2 deletions

View File

@ -525,6 +525,13 @@ modifier comes before the field, not after it::
>>> post.tags >>> post.tags
['database', 'nosql'] ['database', 'nosql']
.. note::
If no modifier operator is specified the default will be `set` the both following sentences are identical::
>>> BlogPost.objects(id=post.id).update(title='Example Post')
>>> BlogPost.objects(id=post.id).update(set__title='Example Post')
.. note:: .. note::
In version 0.5 the :meth:`~mongoengine.Document.save` runs atomic updates In version 0.5 the :meth:`~mongoengine.Document.save` runs atomic updates

View File

@ -164,6 +164,9 @@ def update(_doc_cls=None, **update):
mongo_update.update(value) mongo_update.update(value)
continue continue
parts = key.split('__') parts = key.split('__')
# if there is no operator, default to "set"
if len(parts) < 3 and parts[0] not in UPDATE_OPERATORS:
parts.insert(0, 'set')
# Check for an operator and transform to mongo-style if there is # Check for an operator and transform to mongo-style if there is
op = None op = None
if parts[0] in UPDATE_OPERATORS: if parts[0] in UPDATE_OPERATORS:

View File

@ -1074,11 +1074,13 @@ class InstanceTest(unittest.TestCase):
self.assertRaises(OperationError, update_no_value_raises) self.assertRaises(OperationError, update_no_value_raises)
def update_no_op_raises(): def update_no_op_should_default_to_set():
person = self.Person.objects.first() person = self.Person.objects.first()
person.update(name="Dan") person.update(name="Dan")
person.reload()
return person.name
self.assertRaises(InvalidQueryError, update_no_op_raises) self.assertEqual("Dan", update_no_op_should_default_to_set())
def test_update_unique_field(self): def test_update_unique_field(self):
class Doc(Document): class Doc(Document):