简体   繁体   中英

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

For some reason i want to check the Db as created with Django . 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.

My Question - Why would inspectdb , show the model field differently , how to learn more about this ?

Linked Question - https://stackoverflow.com/a/48739612/4928635

That makes perfect sense, since a FileField is, at the database side, a 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. 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.

The inspectdb tool therefore is not the (perfect) inverse of the migration files that construct the database. inspectdb just makes models that indeed typecheck with the types at the database side. But a Django model is thus more "rich" in terms of logic than the database table counterpart. Usually after running inspectdb it will require some "scaffolding" to ensure that the fields do proper validation, etc.

FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.

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

Please check the docs of Django https://docs.djangoproject.com/en/2.2/ref/models/fields/#filefield

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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