user2125853
user2125853

Reputation: 1315

Django inspectdb generates TextField for MySQL mediumblob

I have a legacy MySQL database. I decided to generate models from the database tables using 'inspectdb'. I have some images stored as mediumblob fields in a table.

I see that Django generated those field as following:

origimage = models.TextField(db_column='OrigImage')  # Field name made lowercase.

Django documentaions says

If inspectdb cannot map a column’s type to a model field type, it’ll use TextField and will insert the Python comment 'This field type is a guess.' next to the field in the generated model.

I do not see the 'This field type is a guess'. Does not appear like Django treated this as a guess. Does Django translate mediumblob to TextField? I would need to see this as a file upload field in the Forms. Am I to change this to an ImageField for the field to be rendered correctly?

Additionally, what would be the right dhango data type to store images in database columns?

Thanks

Upvotes: 2

Views: 726

Answers (1)

atomic77
atomic77

Reputation: 198

It's working as designed. Django's introspection can save you a lot of time, but it can only do so much, and as it also states in the models file that gets generated, you'll have to do some manual cleanup.

Here are some of the mappings that exist for the mysql introspection class taken from the django source code:

    data_types_reverse = {
    FIELD_TYPE.BLOB: 'TextField',
    FIELD_TYPE.CHAR: 'CharField',
    ...
    FIELD_TYPE.TINY_BLOB: 'TextField',
    FIELD_TYPE.MEDIUM_BLOB: 'TextField',
    FIELD_TYPE.LONG_BLOB: 'TextField',
    FIELD_TYPE.VAR_STRING: 'CharField',
}

Notice that none are mapped to a FileField because it would be impossible for the introspection to know what exactly you were using that blob field for.

If you are storing the image content in the database field itself, it might not be a bad idea in any case to migrate to a FileField and have Django manage the storage of the file.

Upvotes: 2

Related Questions