To use Django FileField or FilePathField?

后端 未结 1 924
庸人自扰
庸人自扰 2021-02-05 06:28

I\'m a bit confused about which field to use. What I need is just a Field that will hold a file (audio and/or another one for an image)

The FileField seems to be specifi

1条回答
  •  暖寄归人
    2021-02-05 06:37

    If you want to refer to files that are already on your filesystem rather than user uploaded files, then FilePathField is the one that you want.

    In this regard a few comments:

    • Don't point to a path that is inside your app's source tree. That is not safe.

    • You can use settings or environment variables to handle development vs. production paths, e.g., put a FILE_PATH_FIELD_DIRECTORY setting in your development/production settings files and refer to this setting from your app:

      from django.conf import settings
      
      class Foo(models.Model):
          audio = models.FilePathField(path=settings.FILE_PATH_FIELD_DIRECTORY)
      

    0 讨论(0)
提交回复
热议问题