2👍
✅
You need to implement to_python and get_prep_value in your custom field type to respectively decompress and compress your data.
2👍
Custom field types are definitely the way to go here. This is the only reliable way to ensure that the field is compressed on save and decompressed on load. Make sure you set the metaclass as described in your link.
1👍
I guess it’s worth mentioning that PostgreSQL compresses by default for all string types: Text compression in PostgreSQL
So maybe the answer is: Don’t?
- [Django]-Django validate field based on value from another field
- [Django]-Django class-based views YearArchiveView
- [Django]-How do I resolve access denied aws s3 files?
- [Django]-Django REST Framework serializer – access existing foreign key
- [Django]-Django aggregation over annotated query
0👍
Also see https://djangosnippets.org/snippets/2014/
Seems a bit easier… Still just a TextField under the hood.
class CompressedTextField(models.TextField):
"""
model Fields for storing text in a compressed format (bz2 by default)
"""
__metaclass__ = models.SubfieldBase
def to_python(self, value):
if not value:
return value
try:
return value.decode('base64').decode('bz2').decode('utf-8')
except Exception:
return value
def get_prep_value(self, value):
if not value:
return value
try:
value.decode('base64')
return value
except Exception:
try:
tmp = value.encode('utf-8').encode('bz2').encode('base64')
except Exception:
return value
else:
if len(tmp) > len(value):
return value
return tmp
- [Django]-Django admin: how do I filter a ForeignKeyField widget based on the object's (not request.user's) data?
- [Django]-Django-tables2 – how to use a custom filter in a TemplateColumn
- [Django]-Django Unknown system variable 'TRANSACTION' on syncdb
Source:stackexchange.com