hmarr/mongoengine — pull request #559 by helduel (2012-08-07 15:28:20+00:00)
Title: More precise "created" keyword argument for post_save signal

Hello,

I patched the Document's save-method to look at the updatedExisting-value of MongoDB's response to getlasterror when deciding whether a document has been newly created or updated. This results in a more proper created-keyword argument for post_save-signals.

I had the problem that a document which doesn't use an auto-generated _id value (but has another field as primary key) has never been considered as created in the post_save signal, although it indeed was new. The reason was, that any document that has an _id field is not treated as newly created, unless force_insert is set to True.

My patch works when the safe-argument of the save-method is set to True. Then collection.update returns a dict with a key named "updatedExisting" indicating whether the document has been updated or newly created. This value will be passed to post_save.send.

With kind regards,
Manuel Hermann