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.
- [Answered ]-Alternative of Requests in django
- [Answered ]-Pythonic method for Django loop
- [Answered ]-Django: check whether run as development or production when testing?
- [Answered ]-Django Primary key acting as a foreign key
Source:stackexchange.com