简体   繁体   English

Django-inspectdb-“ models.FileField”显示为“ models.CharField”

[英]Django - inspectdb - the “models.FileField” are displayed as “models.CharField”

For some reason i want to check the Db as created with Django . 由于某些原因,我想检查Django创建的Db。 I run command = python manage.py inspectdb > inspectdb.txt I get to see = models.CharField in place of the expected = models.FileField , whereas the models.py has the correct models.FileField also the Db surely has a models.FileField as am able to store Files = .csv , in this case correctly. 我运行命令= python manage.py inspectdb > inspectdb.txt我能看到= models.CharField到位的预期=的models.FileField ,而models.py有正确的models.FileField还分贝肯定有一个models.FileField能够存储Files = .csv,在这种情况下是正确的。

My Question - Why would inspectdb , show the model field differently , how to learn more about this ? 我的问题-为什么inspectdb会以不同的方式显示模型字段,如何进一步了解这一点?

Linked Question - https://stackoverflow.com/a/48739612/4928635 链接的问题-https: //stackoverflow.com/a/48739612/4928635

That makes perfect sense, since a FileField is, at the database side, a varchar . 这是完全FileField ,因为FileField在数据库端是varchar The database does not store the content of the file. 该数据库存储文件的内容 It stores at the database the path to where the file is stored on the disk (or another storage engine). 它将文件存储在磁盘(或其他存储引擎)上的路径存储在数据库中。

At the database side, there is thus no difference at all, it is only the Django logic that handles it differently. 因此,在数据库方面,根本没有区别,只有Django 逻辑对其进行不同的处理。 If you later analyze the database and aim to generate Django models out of it, then it will of course not by any means see a difference. 如果您以后分析数据库并打算从中生成Django模型,那么它当然不会有任何区别。

The inspectdb tool therefore is not the (perfect) inverse of the migration files that construct the database. 因此, inspectdb工具不是构成数据库的迁移文件的(完全)逆函数。 inspectdb just makes models that indeed typecheck with the types at the database side. inspectdb只是使用数据库端的类型进行确实类型检查的模型。 But a Django model is thus more "rich" in terms of logic than the database table counterpart. 但是,就逻辑而言,Django模型比数据库表对应模型更“丰富”。 Usually after running inspectdb it will require some "scaffolding" to ensure that the fields do proper validation, etc. 通常,在运行inspectdb ,它将需要一些“脚手架”以确保字段进行正确的验证等。

FileField instances are created in your database as varchar columns with a default max length of 100 characters. FileField实例在数据库中创建为varchar列,默认最大长度为100个字符。 As with other fields, you can change the maximum length using the max_length argument. 与其他字段一样,您可以使用max_length参数更改最大长度。

Note that whenever you deal with uploaded files, you should pay close attention to where you're uploading them and what type of files they are, to avoid security holes. 请注意,无论何时处理上传的文件,都应密切注意上传文件的位置以及文件的类型,以避免安全漏洞。 Validate all uploaded files so that you're sure the files are what you think they are Validate所有上载的文件,以确保文件是您认为的样子

Please check the docs of Django https://docs.djangoproject.com/en/2.2/ref/models/fields/#filefield 请检查Django https://docs.djangoproject.com/en/2.2/ref/models/fields/#filefield的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM