[Django]-Django automatically compress Model Field on save() and decompress when field is accessed

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?

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

Leave a comment