Skip to content

Commit

Permalink
Merge pull request #1959 from bagerard/fix_write_concern_default_param
Browse files Browse the repository at this point in the history
Fix but with save(write_concern=None) - introduced in 0.16.1
  • Loading branch information
erdenezul committed Nov 21, 2018
2 parents 3627969 + fcbabbe commit 220513a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Development
===========
- (Fill this out as you fix issues and develop your features).

=================
Changes in 0.16.2
=================
- Fix .save() that fails when called with write_concern=None (regression of 0.16.1) #1958

=================
Changes in 0.16.1
=================
Expand Down
5 changes: 4 additions & 1 deletion mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def modify(self, query=None, **update):
return True

def save(self, force_insert=False, validate=True, clean=True,
write_concern={'w': 1}, cascade=None, cascade_kwargs=None,
write_concern=None, cascade=None, cascade_kwargs=None,
_refs=None, save_condition=None, signal_kwargs=None, **kwargs):
"""Save the :class:`~mongoengine.Document` to the database. If the
document already exists, it will be updated, otherwise it will be
Expand Down Expand Up @@ -361,6 +361,9 @@ def save(self, force_insert=False, validate=True, clean=True,
if validate:
self.validate(clean=clean)

if write_concern is None:
write_concern = {'w': 1}

doc = self.to_mongo()

created = ('_id' not in doc or self._created or force_insert)
Expand Down
8 changes: 6 additions & 2 deletions tests/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,17 @@ def test_update_write_concern(self):
self.Person.drop_collection()

write_concern = {"fsync": True}

author = self.Person.objects.create(name='Test User')
author.save(write_concern=write_concern)

# Ensure no regression of #1958
author = self.Person(name='Test User2')
author.save(write_concern=None) # will default to {w: 1}

result = self.Person.objects.update(
set__name='Ross', write_concern={"w": 1})
self.assertEqual(result, 1)

self.assertEqual(result, 2)
result = self.Person.objects.update(
set__name='Ross', write_concern={"w": 0})
self.assertEqual(result, None)
Expand Down

0 comments on commit 220513a

Please sign in to comment.