How to customize pickle for django model objects

Posted by muudscope on Stack Overflow See other posts from Stack Overflow or by muudscope
Published on 2010-03-15T14:58:59Z Indexed on 2010/03/15 15:19 UTC
Read the original article Hit count: 428

Filed under:
|
|

I need to pickle a complex object that refers to django model objects. The standard pickling process stores a denormalized object in the pickle. So if the object changes on the database between pickling and unpickling, the model is now out of date. (I know this is true with in-memory objects too, but the pickling is a convenient time to address it.)

So what I'd like is a way to not pickle the full django model object. Instead just store its class and id, and re-fetch the contents from the database on load. Can I specify a custom pickle method for this class? I'm happy to write a wrapper class around the django model to handle the lazy fetching from db, if there's a way to do the pickling.

© Stack Overflow or respective owner

Related posts about django

Related posts about python