Search code examples
pythondjangoinspectdb

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


Solution

  • 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.