重命名 django FileField 文件
django 应用程序中添加了一些附加功能,因此 upload_to 功能也得到了扩展。
由于 django 默认将文件名存储在数据库中,文件存储在磁盘上,因此不会造成任何损害 - 新文件使用新的 upload_to 函数命名,而旧文件继续工作。
然而,这很混乱 - 我们最终会遇到这样的情况:
/media/userID/oldfilename.pdf
有
/media/app/userID/projectID/newfilename.pdf
没有办法批量重命名这些文件?我想这可以通过遍历数据库来完成,检查 FileField 中的路径是否与当前 upload_to 的结果匹配,如果不匹配,则重命名..这似乎是一个常见问题,所以也许有更通用的方法?
Some additional features were added to a django application, and as a result the upload_to function also expanded.
Since django by default stores filenames in the database, and files on disk, no harm has been done - new files are named using a new upload_to function, while old files continue to work.
However, this is messy - we end up in a situation with files like
/media/userID/oldfilename.pdf
and
/media/app/userID/projectID/newfilename.pdf
Is there a way to bulk rename those files? I guess it could be done by iterating through the database, checking if the path in FileField matches the result of current upload_to, and if not, rename.. it seems like a common problem, so perhaps there is a more generic way out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的解决方案是编写自定义 Django 管理命令。您可以使用 Django 的标准
manage.py
运行该命令。像这样的东西:
The simple solution is to write a custom Django management command. You can run the command using Django's standard
manage.py
.Something like this: