-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field values are lost when pickling jsonmodel #121
Comments
Thanks for the 'getstate()' and 'setstate()' information. I was using jsonmodels with python multiprocessing I'm now using this hack (runtime patching of def getstate(self):
state = self.to_struct()
state['_cache_key'] = self._cache_key
return state
models.Base.__getstate__ = getstate
def setstate(self, data):
self._cache_key = data.pop('_cache_key')
self.populate(**data)
models.Base.__setstate__ = setstate PS: there's a similar issue with deepcopy |
Hi, I was able to omit this behavior by adding an inherited constructor: class TestModel(models.Base):
a = fields.StringField(default="")
def __init__(self, *args, **kwargs):
super(TestModel, self).__init__(*args, **kwargs) Afterwards, the error above disappeared. |
I found out that using pickle serializer does not seem to work with jsonmodels. Using this simple model as an example (using Python 2.7.10):
I'm using Python 2.7.10.
So the field value is lost when pickling. I found out that pickle uses
__dict__
variable by default, and as this does not contain field data, it is not saved to the serialized form.Using customized
__getstate__()
and__setstate__()
methods I managed to make it work, I just had to make sure the_cache_key
variable is preserved, as unpickling the object does not run__init__()
and_cache_key
is not created:Does this seem to be a correct way to handle pickling? Also, is it necessary to pickle the
_cache_key
, or would it be ok to just initialize it to a new value? (Some kind of_init_cache_key()
method would be a good addition in that case).If this is a correct direction, I can make a pull request and test it with other Python versions. Support for JSON serializer would also be possible.
The text was updated successfully, but these errors were encountered: