我在Django 1.6中使用了一个函数,在通过admin上传文件时重命名我的文件,但这在Django 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'))