Given the Django model:
from django.db import models
class MyModel(models.Model):
textfield = models.TextField()
How can I automatically compress textfield(for example zlib) on save()and unpack it when a property textfield(i.e. not when loading), with such a workflow:
m = MyModel()
textfield = "Hello, world, how are you?"
m.save()
m.textfield
id = m.id()
m = MyModel.get(pk=id)
m.textfield
I would be inclined to think that you overloaded MyModel.save, but I do not know a template for modifying an element in place while saving. I also don't know how the best way in Django to unpack when a field is at its access (overload __getattr__?).
Or is the best way to do this: a custom field type ?
I am sure that I saw an example of almost this, but, alas, I could not find it recently.
Thanks for reading - and for any input you can provide.