More precise "created" keyword argument signals

If a document has a user given id value, the post_save signal always got the
"created" keyword argument with False value (unless force_insert is True).

This patch uses the result of getlasterror to check whether the save was an
update or not.
This commit is contained in:
helduel
2012-11-08 16:30:29 +01:00
parent c31488add9
commit 1a93b9b226
2 changed files with 44 additions and 4 deletions

View File

@@ -233,13 +233,24 @@ class Document(BaseDocument):
actual_key = self._db_field_map.get(k, k)
select_dict[actual_key] = doc[actual_key]
def is_new_object(last_error):
if last_error is not None:
updated = last_error.get("updatedExisting")
if updated is not None:
return not updated
return created
upsert = self._created
if updates:
collection.update(select_dict, {"$set": updates},
upsert=upsert, safe=safe, **write_options)
last_error = collection.update(select_dict,
{"$set": updates}, upsert=upsert, safe=safe,
**write_options)
created = is_new_object(last_error)
if removals:
collection.update(select_dict, {"$unset": removals},
upsert=upsert, safe=safe, **write_options)
last_error = collection.update(select_dict,
{"$unset": removals}, upsert=upsert, safe=safe,
**write_options)
created = created or is_new_object(last_error)
warn_cascade = not cascade and 'cascade' not in self._meta
cascade = (self._meta.get('cascade', True)