I'm currently trying to pickle certain Django models. I created a __getstate__
and __setstate__
method for the model, but it looks like pickle.dumps()
is using the default __reduce__
instead.
Is there a way to force use of __getstate__
and __setstate__
? If not, what is the best way to overwrite __reduce__
?
I am currently using Django 1.6 and Python 2.7.6, if that helps.
In essence, I am using get and set state to remove two fields before pickling in order to save space.
While it's not really the "answer" to my question, the best solution I found was to implement a dehydrate()
method on the model, allowing me to alter the model's __dict__
and store that instead.
On recovery from the cache, it's as simple as using the **
syntax and you'll have your original model back.
Using pickle getstate/setstate you can have your models automatically pickle just the primary key and use it to load when unpickle.
https://docs.python.org/2/library/pickle.html#object. getstate
Like so:
class FooModel(Model):
field1 = CharField()
def __getstate__(self):
return self.pk
def __setstate__(self, state):
self.pk = state
self.refresh_from_db()
This let's you pickle django models and all it stores is the primary key. Then when you unpickle it it does a fetch for the fields using the primary key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.