简体   繁体   English

使用Django将上传文件重命名为admin

[英]Rename file on upload to admin using Django

I have used a function in Django 1.6 to rename my files when they are uploaded through admin, but this does not work in Django 1.8. 通过admin上传文件时,我在Django 1.6中使用了一个函数来重命名我的文件,但这在Django 1.8中不起作用。 Anyone know if it is still possible to do this in 1.8? 有人知道在1.8中是否仍有可能吗?

class Entry(models.Model):

    def path_and_rename(path):
        def wrapper(instance, filename):
            ext = filename.split('.')[-1]
            # get filename
            if instance.pk:
                filename = "%s-%s.%s" % (instance.pub_date.year,instance.issue, ext)
            else:
                # set filename as random strin
                filename = "%s.%s" % (uuid.uuid4(), ext)    
            # return the whole path to the file
            return os.path.join(path, filename)
        return wrapper


    name = models.CharField(max_length=500)
    pub_date = models.DateTimeField()
    issue = models.PositiveIntegerField()  

    pdf = models.FileField(blank=True, upload_to=path_and_rename('uploads/pdf'))

Maybe you need to change the way file uploaded by using the fileField . 也许您需要使用fileField更改文件上传的方式。 Here is link for you. 这是给你的链接

When function upload_file gets called, you can rename the file in this function. 调用函数upload_file时,可以在此函数中重命名文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM