gpt4 book ai didi

Django:输入图像文件的小写文件名

转载 作者:行者123 更新时间:2023-12-01 11:07:54 24 4
gpt4 key购买 nike

我有一个模型:

class Foo(models.Model):
poster = models.ImageField(u"Poster", upload_to='img')

我正在使用管理员上传海报并保存 Foo 对象。我现在需要找到一种在保存前将文件名小写的方法。例如 POSTER.png 或 Poster.png 或 poster.PNG 应小写为 poster.png。

实现此目标的最简单方法是什么?

最佳答案

FileField.upload_to根据文档中的这条评论,也可能是可调用的:

This may also be a callable, such as a function, which will be called to obtain the upload path, including the filename. This callable must be able to accept two arguments, and return a Unix-style path (with forward slashes) to be passed along to the storage system. The two arguments that will be passed are:

ImageFieldFileField 继承它的所有属性和方法,我想你可以使用:

def update_filename(instance, filename):
path_you_want_to_upload_to = "img"
return os.path.join(path_you_want_to_upload_to, filename.lower())

class Foo(models.Model):
poster = models.ImageField(u"Poster", upload_to=update_filename)

关于Django:输入图像文件的小写文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3456374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com