Repository: MongoEngine/mongoengine

Issue:
Errors on save with 0.16.1
Hi folks,

Just updated to 0.16.1 and started getting weird errors on save. Here's one example on inserting a new doc, traceback message:

```
e.save()
save_condition, signal_kwargs, **kwargs)
self._save_create(doc, force_insert, write_concern)
set_write_concern(collection, write_concern) as wc_collection:
next(self.gen)
combined_concerns.update(write_concerns)

TypeError: 'NoneType' object is not iterable
```

Also seems to happen on update.

Switching back to 0.16.0 made the errors go away.


Discussion:
Hi @mikeckennedy, The code snippet you provided looks malformed, can you provide another snippet that reroduce the issue?
BTW thanks for the podcasts :) !
Hi @bagerard That's just the traceback frame. Not really executable code.

Thanks for the kind words on the podcast! :)
That is a little confused, I think I over redacted it. Here's a screenshot from Rollbar:

![screen shot 2018-11-20 at 2 13 06 pm](https://user-images.githubusercontent.com/2035561/48806202-724f0700-ecce-11e8-827c-78bb3604d380.png)

I assume you are providing `write_concern` parameter to the .save() method, what value are you using for write_concern? Could it be that it's None?
ok got it, this change is the thing that break in your case
https://github.com/MongoEngine/mongoengine/commit/611094e92e7994c03ba54e68ded8a9a2d7db9175#diff-b3cd585db6ec2d41acd04ddd897c23e8
We'll probably merge a fix very shortly and release a 0.16.2 as it's a bad bug
Wow, you guys are quick. Thanks a bunch Bastien. I didn't think I changed anything :)

Resolving patch:
diff --git a/mongoengine/document.py b/mongoengine/document.py
--- a/mongoengine/document.py
+++ b/mongoengine/document.py
@@ -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
@@ -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)


Test patch:
diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py
--- a/tests/queryset/queryset.py
+++ b/tests/queryset/queryset.py
@@ -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)