I've run into a situation where I have binary data (created using os.urandom(BLOCK_SIZE)
) that I need to store in a Postgres database using Django Models.
I've read several references to how Django doesn't yet support Bytea (VARBINARY) field types yet. I found a reference here but don't know where to get that library (django_postgresql).
I'm currently using Python 2.7 and Django 1.4 on Ubuntu 12.04 - if that helps.
I've read some suggestions as a way around this is to convert my data to hex before storing it.
import binascii
key = binascii.hexlify(value)
Ok, now what? I tried to save this value to the database but get errors:
invalid byte sequence for encoding "UTF8": 0xd6c2
Ok, which data field type should I use to do this?
key = models.TextField(max_length = 200)
?
key = models.CharField(max_length = 200)
?
key = models.???(max_length = 200)
???
I'd like to stay away from creating my own custom field type. First, I'm pretty new to Django and wouldn't feel comfortable doing this yet. Second, I'm pretty new to databases so I'm not sure where to start on defining something like this.
Any suggestions?
BLOCK_SIZE
and is it likely to change? – Jon Clements Jul 5 '12 at 20:28BLOCK_SIZE
in this case is 32 Bytes (os.urandom(32)
). TheBLOCK_SIZE
will not change. – Rico Jul 5 '12 at 20:32type(key)
isstr
orunicode
. You may need tokey.encode('ascii')
if unicode... – Jon Clements Jul 5 '12 at 20:41