In Google App Engine, what happens when I change the Class related to a persisted object?
My model class looks as follows:
from google.appengine.ext import db
class SnapShotBase(db.Model):
'''
The base class from which all entity snapshots will inherit.
'''
version = db.IntegerProperty()
def __init__(self):
pass
Imagine I alread开发者_如何学编程y have persisted instances of this class in my bigtable datastore. If I were to ADD a field to this class, would it break deserialization? Or would the new properties simply remain empty?
Model instances aren't stored using standard serialization such as Pickle. The properties (such as 'version' in your example) are encoded and stored as a Protocol Buffer, and when you load an entity from the datastore, the Protocol Buffer is decoded and used to build a new Model instance.
As a result, you can modify your object however you like. Adding new properties will cause them to have their default value for any entities that were stored before they were added, or to throw an error if the new property is required and no default is supplied. Removing fields will simply cause them to no longer show up on your model instances.
One warning, however: You shouldn't be overriding init in your model classes, as you do above. Doing so is likely to break construction of entities from the datastore. If you need to modify the construction behaviour, I'd suggest using a factory method (or function) instead.
精彩评论