本文介绍了Django inspectdb为MySQL mediumblob生成TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧版MySQL数据库.我决定使用'inspectdb'从数据库表生成模型.我有一些图像存储为表格中的blobblob字段.

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.

我看到Django生成了以下字段:

I see that Django generated those field as following:

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

Django文档说

我看不到此字段类型是猜测".看起来不像Django将其视为一种猜测. Django是否将mediumblob转换为TextField?我需要将其视为表单中的文件上传字段.我是否要将其更改为ImageField以使字段正确呈现?

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?

另外,将图像存储在数据库列中的正确dhango数据类型是什么?

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

谢谢

推荐答案

它按设计工作. Django的自省功能可以为您节省很多时间,但是它只能做很多事情,而且它还会在生成的模型文件中指出,您必须进行一些手动清理.

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.

以下是取自django源代码的mysql自省类:

    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',
}

请注意,没有一个映射到FileField,因为内省不可能知道您正使用该Blob字段确切地做什么.

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.

如果您将图像内容存储在数据库字段本身中,那么在任何情况下迁移到FileField并使用Django 管理文件的存储空间.

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.

这篇关于Django inspectdb为MySQL mediumblob生成TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:16