Zlib in a database - Django

When I try to put a zlibbed string in models.TextField

>>> f = VCFile(head = 'blahblah'.encode('zlib'))
>>> f.save()

      

he does not work:

    ...
raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 1: unexpected code byte. You passed in 'x\x9cK\xcaI\xccH\x02b\x00\x0eP\x03/' (<type 'str'>)

      

Is there a way to fix this (other than string escaping - it should be space efficient)?

+1


a source to share


2 answers


As Markus says, you will have to use BLOB if you want to store it in binary. If you are ok with the encoding you can use base64 encoding:

from base64 import binascii

f = VCFile(head = binascii.b2a_base64('blahblah'.encode('zlib')))

      



In my basic tests with 33k characters, the zlib string was 28% of the original string size, the zlib base64 encoded string was 37% of the original string size. Not quite as good in compression, but still a big improvement.

+1


a source


If you don't want to encode it, you must store it as a Binary Object (BLOB), not a string. Django doesn't seem to support BlobFields out of the box, so find one online or hack something together.



0


a source







All Articles