这就是我的尝试:
r = requests.get(img_url)
temp = NamedTemporaryFile(delete=True)
temp.write(r.content)
temp.flush()
image = Image()
image.image.save('testimagefilename', File(temp), save=True)
image.save()
在image.image.save()
行上,错误跟踪为:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 90, in save
name = self.field.generate_filename(self.instance, name)
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 332, in generate_filename
return os.path.join(self.get_directory_name(), self.get_filename(filename))
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 322, in get_filename
return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
TypeError: get_valid_name() missing 1 required positional argument: 'name'
问题是因为Django期待本地操作系统路径吗?我正在使用S3BotoStorage,ImageField正在使用项目其余部分的独立存储桶:
image = models.ImageField(storage=S3CustomStorage)
class S3CustomStorage(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket'] = getattr(settings, 'AWS_BUCKET_CUSTOM')
super(S3CustomStorage, self).__init__(*args, **kwargs)
答案 0 :(得分:9)
也许请参阅Trying to understand Django source code and cause of missing argument TypeError并更改
image = models.ImageField(storage=S3CustomStorage)
到
image = models.ImageField(storage=S3CustomStorage())
我有相同的TypeError ;-)但现在它适用于我的情况下的additional()