标签:python django django-models django-views django-forms
是否可以在django中更改上传文件的文件名?我搜索过,但找不到任何答案.
我的要求是每当上传文件时,其文件名应以下列格式更改.
format = userid + transaction_uuid + file_extension
非常感谢你…
解决方法:
你是如何上传文件的?
我假设使用FileField.
FileField.upload_to的文档说upload_to字段,
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:“instance“: An instance of
the model where theFileField
is
defined. More specifically, this is
the particular instance where the
current file is being attached.“filename“:The filename that was
originally given to the file. This may
or may not be taken into account when
determining the final destination
path.
所以看起来你只需要创建一个函数来处理你的名字并返回路径.
def update_filename(instance, filename):
path = "upload/path/"
format = instance.userid + instance.transaction_uuid + instance.file_extension
return os.path.join(path, format)
标签:python,django,django-models,django-views,django-forms 来源: https://codeday.me/bug/20190923/1815232.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。