4
votes

My app uses a "per-user session" to allow multiple sessions from the same user to share state. It operates very similarly to the django session by pickling objects.

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.)

Clearly it would be cleaner to store this complex in the database, but it's not practical. The code for it is necessarily changing rapidly as the project evolves. Having to update the database schema every time the object's data model changes would slow the project down a lot.

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.

2
Why do you want to use pickle to save your data? Can you give more detail? It sounds to me like you'd probably be better off saving your data in the db - Zach
Yes, it would be cleaner to store this in the database, but it's not practical. The object I'm preserving is very complex and necessarily changing rapidly as the project evolves. Having to update the database schema every time I change anything about what this object stores would grind the project to a halt. - muudscope
Please update your question with additional facts. You own the question; you're not limited to stringing comments together. Please update the question. Please explain why JSON is not appropriate for your problem. - S.Lott

2 Answers

1
votes

It's unclear what your goal is.

"But if I just store the id and class in a tuple then I'm necessarily going back to the database every time I use any of the django objects. I'd like to be able to keep the ones I'm using in memory over the course of a page request."

This doesn't make sense, since a view function is a page request and you have local variables in your view function that keep your objects around until you're finished.

Further, Django's ORM bas a cache.

Finally, the Django-supplied session is the usual place for "in-memory objects" between requests.

You shouldn't need to pickle anything.

0
votes

You can overload the serialization methods. But it would be simpler to put the id and class in a tuple or dict and pickle that.