我需要在调用 super() 方法之前保存上传的文件。它应该被保存,因为我使用一些外部工具将文件转换为所需的内部格式。以下代码在上传文件“123”时产生错误:
OSError: [Errno 36] File name too long: '/var/www/prj/venv/converted/usermedia/-1/uploads/123_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_...'
似乎,它试图super().save()
在无限循环中以相同的名称将其保存两次。此外,它还会创建所有这些文件。
def save(self, **kwargs):
uid = kwargs.pop('uid', -1)
for field in self._meta.fields:
if hasattr(field, 'upload_to'):
field.upload_to = '%s/uploads' % uid
if self.translation_file:
self.translation_file.save(self.translation_file.name, self.translation_file)
#self.mimetype = self.guess_mimetype()
#self.handle_file(self.translation_file.path)
super(Resource, self).save(**kwargs)
编辑:
这是我想绕过的不优雅的方式(它将双重调用save()
方法):
def save(self, *args, **kwargs):
uid = kwargs.pop('uid', -1)
for field in self._meta.fields:
if hasattr(field, 'upload_to'):
field.upload_to = '%s/uploads' % uid
super(Resource, self).save(*args, **kwargs)
if self.__orig_translation_file != self.translation_file:
self.update_mimetype()
super(Resource, self).save(*args, **kwargs)