Search code examples
djangooracle-databaseormblobblobstorage

reading raw binary data from blob columns using oracle backend


I'm working with oracle legacy DB and need to read and write binary data (png images, and and MDL Molfiles). Django's inspectdb command generated text fields for these columns, saying that's only a guess. Now, when I'm trying to retrieve a value from this class fields i get: DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte. error.

Is there any way to read and write these columns? Any help would be appreciated.


Solution

  • class BlobField(models.TextField):
        description = "Stores raw binary data"
    
        __metaclass__ = models.SubfieldBase
    
        def __init__(self, *args, **kwds):
            kwds = _adjust_keywords(kwds)
            super(BlobField, self).__init__(*args, **kwds)
    
        def get_internal_type(self):
            return "BlobField"
    
        def get_db_prep_value(self, value, connection=None, prepared=False):
            return value
    
        def to_python(self, value):
            return Blob(value)