Django SVN-release can’t handle binary data insertion?
Django does not have a BinaryField, or BlobField or whatever you want to call it, which is a bit sad. I made a fix for this a few posts back that worked fine in 0.96. However when trying the SVN-release it seems like everything that goes to the database is first turned into unicode, which of course includes the binary data.
Even doing custom SQL won’t work
An example from my code.
The info_hash field is a blob
def create_xbt_file(info_hash, timestamp): query = "INSERT INTO xbt_files (info_hash, mtime, ctime) VALUES (%s, %s, %s)" from django.db import connection cursor = connection.cursor() cursor.execute(query, [info_hash, timestamp, timestamp])
This throws a nasty UnicodeDecodeError whenever a byte with a position that unicode doesn’t like is in info_hash.
If I bypass django completely using MySQLdb, then it works fine
def create_xbt_file(info_hash, timestamp):
import MySQLdb
db = MySQLdb.connect("localhost", DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME)
cursor = db.cursor()
query = "INSERT INTO xbt_files (info_hash, mtime, ctime) VALUES (%s, %s, %s)"
cursor.execute(query, [info_hash, timestamp, timestamp])
db.close()
This obviously is a ugly hack, but it seems to be the only way to fix it right now.
If I’m wrong then please correct my since I would obviously not like to have this in my code.
Recent Comments