Geekish Stuff

Django model Oracle BLOB update field

Date published: 4-Jan-2012
1 min read / 127 words
Author: braceta

Programming
Django
Programming
Python

Today I was using Django 1.3 with an existing Oracle database and got the following Oracle error in Django Admin when trying to save a form that updates a model class that refers to a BLOB type column:

ORA-01465: invalid hex number

The afforementioned model was using a simple models.TextField that works fine to display the BLOB data (currently holding XML data). So my approach to fix this problem was to create a custom column Field for my model that extended this class models.TextField. I've used the following code:

class OracleBlobField(models.TextField):
def get_placeholder(self, value, connection):
return "UTL_RAW.CAST_TO_RAW(%s)"

With this code, your model's SQL UPDATE will convert the String you are submitting into a raw value in Oracle that saves the data correctly.

Then on you Django Model just use it, such as:

object_blob = OracleBlobField()

Hope it helps.