我需要保存上传文件之前 super()方法被调用。它应该被保存,因为我使用一些外部应用程序将文件转换为所需的内部格式。下面的代码产生一个错误上载文件“123”:Django:在调用超级()之前保存FileField()
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)
translation_file.save()做什么?在这个例子中,我没有看到会导致递归的原因。 – jdi
'translation_file'被定义为FileField。但方法被引用到'django.db.models.fields.files.py'中的'FieldFile'。 'save()'被定义为'def save(self,name,content,save = True):' –