2

I want to create a table like so -

CREATE TABLE trial_xml (
id int(11) DEFAULT NULL,
pid int(11) DEFAULT NULL,
sid varchar(256) CHARACTER SET utf8 NOT NULL,
data blob,
PRIMARY KEY (soid), KEY suid_index (suid) ) ENGINE=MyISAM DEFAULT CHARSET=latin1

my question is how do I set "data" field as "blob" in django's models.py ??

I mean what's the syntax?

UPDATE: I dont want to set data field as longtext. I want only blob datafield.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264

3 Answers3

2

I have been using this simple field for 'mysql' backend, you can modify it for other backends

class BlobField(models.Field):
    description = "Blob"
    def db_type(self):
        return 'blob'
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • This is good for reading data. Unfortunately if you want to write BLOB data on Oracle backend you have to do something similar to this: http://code.google.com/p/cx-oracle-demos/source/browse/trunk/blob.py?r=11. Unfortunately I don't know how to incorporate it in django code. – mnowotka Dec 06 '12 at 17:06
1

For what it's worth, Django now has a proper BinaryField. It was added on Dec 13, 2012.

https://github.com/django/django/commit/8ee1eddb7e148de89aebde9e68da495633fc1ec9

The relevant documentation is available here: https://docs.djangoproject.com/en/1.8/ref/models/fields/#binaryfield

infinityzxx
  • 319
  • 1
  • 9
1

Django's ORM has no field for binary large objects. Either use something like a FileField, or search for candidate field classes using a search engine.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358