总之,我在这里要完成的是:
假设您有一个带有徽标的Company
型号。像
class Company(models.Model):
name = ...
logo = models.FileField(blank=True, null=True)
现在,因为我有点偏执,我不喜欢上传文件得到(可能是邪恶的)用户给出的名字这一事实,我添加一个upload_to
参数指向一个函数这就像
def logo_getfilename(instance, filename):
extension = ... # get just the original extension from the file
return 'logos/' + str(uuid.uuid4()) + extension
好的,现在只缺少视图了!
def company_edit(request, ...):
company = ... # get the company and stuff
if request.method == 'POST':
form = CompanyAdminForm(request.POST, request.FILES, instance=company)
last_file_path = None
if not company.logo is None:
last_file_path = company.logo.path
# ^^ after calling is_valid(), file_path gets changed to
# the would-be-default-behavior
if form.is_valid():
# first we write the new file
form.save()
# now we remove the old one
os.unlink(last_file_path)
虽然目前正在使用,但我对此并不满意,因为
os.unlink()
代替FieldFile.delete()
似乎错了form.save()
将处理所有内容.save()
模型更改提交到数据库后才应删除以前的文件为了实现这些简单(而不是那种不常见)的目标,您的建议是什么?
答案 0 :(得分:0)
为什么不使用公司的主键,而不是随机用户名?当他们上传新文件时,只需覆盖现有文件(我认为只要您在保存之前更改文件名,它就会自动执行)。这样就无需执行os.unlink
和(可能)停止您的交易担忧。