Running code when mongoengine loads a document
We are trying to run code that modifies a document when it loads as part of schema maintenance. We have a document such as from mongoengine import Document from mongoengine.fields import IntField, StringField class User(Document): version = IntField(default=0) name = StringField() Instances of User are created with version=1 and saved. Later on, we modify this class as follows: class User(Document): version = IntField(default=0) name = StringField(max_length=20) Some of the version 1 documents now need to be truncated: def upgrade_1_to_2(self): self.name = self.name[:20] We want to automatically run that function whenever a User is retrieved from the database, and only if version == 1. (Future upgrades would be upgrade_2_to_3(), and so on.) Where in this API can I put code that runs when a document is retrieved?
Looks like it's a good candidate for the post_init signal and you can do a check there. See: http://docs.mongoengine.org/guide/signals.html
How to update Saved Document Extra data in GridFS
How do I deal with Error stated below?
pymongo query return in direct query not in pymongo
Displaying nested dictionaries in flask-admin
mongoengine slow serialization of embedded documents with reference fields
Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb