Django:如何在FileField中保存原始文件名? [英] Django: How to save original filename in FileField?

查看:79
本文介绍了Django:如何在FileField中保存原始文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要文件名是随机的,因此我使用 upload_to 函数返回随机文件名,如下所示:

 从uuid import uuid4 
import os
def get_random_filename(instance,filename):
ext = filename.split('。')[ - 1 ]
filename =%s。%s%(str(uuid4()),ext)
return os.path.join('some / path /',filename)

#里面的模型
class FooModel(models.Model):
file = models.FileField(upload_to = get_random_filename)

但是,我想保存原始文件名到模型中的属性。这样的事情不起作用:

  def get_random_filename(instance,filename):
instance.filename = filename
ext = filename.split('。')[ - 1]
filename =%s。%s%(str(uuid4()),ext)
return os.path.join ('some / path /',filename)

#里面的模型
class FooModel(models.Model):
file = models.FileField(upload_to = get_random_filename)
filename = models.CharField(max_length = 128)

我该怎么做? >

谢谢。

解决方案

发布的代码通常工作,也许是实际的代码是

  class FooModel(models.Model):
filename = models.CharField(max_length = 128)
file = models.FileField(upload_to = get_random_filename)

注意切换上述字段的顺序。



这不行,因为: upload_to() pre_save()这里的代码,当需要 FileField 的实际值时。您可以发现, upload()中的属性 filename 的赋值是在生成第一个参数 filename 在插入sql。因此,赋值在生成的SQL中不起作用,仅影响实例本身。



如果没有问题,请发布您在shell中键入的代码。 / p>

I want the filenames to be random and therefore I use upload_to function which returns a random filename like so:

from uuid import uuid4
import os
def get_random_filename(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (str(uuid4()), ext)
    return os.path.join('some/path/', filename)

# inside the model
class FooModel(models.Model):
    file = models.FileField(upload_to=get_random_filename)

However I would like to save the original filename to an attribute inside the model. Something like this does not work:

def get_random_filename(instance, filename):
    instance.filename = filename
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (str(uuid4()), ext)
    return os.path.join('some/path/', filename)

# inside the model
class FooModel(models.Model):
    file = models.FileField(upload_to=get_random_filename)
    filename = models.CharField(max_length=128)

How can I do it?

Thank you.

解决方案

The posted code normally works, perhaps the actual code is

class FooModel(models.Model):
    filename = models.CharField(max_length=128)
    file = models.FileField(upload_to=get_random_filename)

Note the switching of the ordering of the fields above.

This won't work because: the upload_to() is invoked by the pre_save(), here in the code, when the actual value of the FileField is required. You could find that the assignment to the attribute filename in the upload() is after the generating of the first param filename in the inserting sql. Thus, the assignment does not take effect in the generated SQL and only affects the instance itself.

If that's not the issue, please post the code you typed in shell.

这篇关于Django:如何在FileField中保存原始文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆