且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django:如何在FileField中保存原始文件名?

更新时间:2022-10-23 20:10:10

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

  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.