开发者

Modify Filename Before Saving in Django using model.FileField

I have a model.FileField(upload_to='%Y/%m/%d') field. This works great; however, I want to rename the file based on the context of the user uploading the file before it is saved. Is there a way of modifying the request object before it saved to accomplish this?

I have come across people with similar issues but the answers always point back to the Django documentation. I hav开发者_如何学编程e tried figuring this out using the documentation but can't. Can someone provide some code to show hot to solve this?

Thanks in advance.


you can use function for upload_to value with instance and filename inputs and return path and file name

for example:

def upload_to_func(instance, filename):
    now = datetime.now()
    return os.path.join(str(now.year), str(now.month), str(now.day), filename)



field_x = model.FileField(upload_to=upload_to_func)

you can change path and filename in function

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜