[Answered ]-How to Pickle Django Model

2đź‘Ť

âś…

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.

0đź‘Ť

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.

Leave a comment