'How to handle possible race-condition in overwritten Document.save method - Mongoengine
I am using flask-mongoengine and think I am running in some kind of race conditions while trying to overwrite the Document.save method.
My models (simplified) look like this:
class User(Document):
meta = {"collection": "users"}
name = StringField()
class Group(Document):
meta = {"collection": "groups"}
name = StringField()
class History(EmbeddedDocument):
key = StringField()
oldValue = StringField()
newValue = StringField()
class Asset(DynamicDocument):
meta = {"collection": "assets"}
c_id = SequenceField()
name = StringField()
history = ListField(EmbeddedDocumentField(History))
user = ReferenceField('User')
group = ReferenceField('Group', required=True, default=Group.objects.first())
def save(self, **kwargs):
for key, value in self._data.items():
history_update = History(
key=key,
oldValue="",
newValue=str(value)
)
self.history.append(history_update)
return super(Asset, self).save(**kwargs)
What I am trying to achieve is:
When a new Document of type Asset is created, add an entry of type History for each Key/Value pair of the document that changed. (Here from None to some value, I have similar code in the update method for changes on existing assets). This history list should be something like a changelog of the particular asset through its lifetime.
My problem with the current implementation is that:
c_idof typeSequenceFieldisNonein my for-loop.str(value)for theUserobject gives me the correct user-object (or the result of my custom__str__method) butstr(value)for theGroupobject gives meDBRef('groups', '<mongoidstring>')and does not trigger my customer str method- When debugging with a breakpoint beforehand, these two errors do not occur.
c_idhas its correct value and mygroupobject is a group object and not aDBRefobject
I've tried saving the Document once before and then adding my history which at least gives me a correct c_id but the group is still a DBRef.
I do think the SequenceField is populated in parallel and therefore still None when I try to access it but not when I come through the debugger. But the DBRef still gives me headaches. And that I don't really see a way to properly implement my ChangeHistory through overwriting the save method. Any ideas how to properly handle this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
