Choose the filename of an uploaded file with Django

后端 未结 2 1018
南笙
南笙 2021-01-12 02:41

I\'m uploading images (represented by a FileField) and I need to rename those files when they are uploaded.

I want them to be formated like that:

\"%d-

相关标签:
2条回答
  • 2021-01-12 03:40

    You don't need to write your own FileStorage class or anything that complicated.

    The 'upload_to' parameter on File/ImageFields can take a function that returns the path/file to use.

    How to do this has already been answered here

    0 讨论(0)
  • 2021-01-12 03:41

    My initial instinct when reading this was that you need to overload the save method on the model, and use the os.rename() method, but that causes a lot of overhead, and is just generally a hassle from start to finish. If you simply want to rename the file, but don't want to make any physical changes to it (resizing, duplicating, etc.), then I'd definitely recommend the approach arcanum suggests above.

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