Django ImageField change file name on upload

前端 未结 5 1563
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 04:35

Upon saving me model \'Products\' I would like the uploaded image to be named the same as the pk for example 22.png or 34.gif I don\'t want to change the format of the image jus

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 04:54

    Another option, as following this answer https://stackoverflow.com/a/15141228/3445802, we found the issue when we need return path with %Y/%m/%d, example:

    FileField(upload_to=path_and_rename('upload/here/%Y/%m/%d'), ...)
    

    so, we handle it with this:

    FileField(upload_to=path_and_rename('upload/here/{}'.format(time.strftime("%Y/%m/%d"))), ...)
    

    Makesure the module time has been imported.

提交回复
热议问题