这是我尝试过的:
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 "", line 1, in 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)
andreas.natu.. 9
也许看到试图理解Django源代码以及缺少参数TypeError和更改的原因
image = models.ImageField(storage=S3CustomStorage)
至
image = models.ImageField(storage=S3CustomStorage())
我有相同的TypeError ;-)但现在它适用于我的情况下的additional()
也许看到试图理解Django源代码以及缺少参数TypeError和更改的原因
image = models.ImageField(storage=S3CustomStorage)
至
image = models.ImageField(storage=S3CustomStorage())
我有相同的TypeError ;-)但现在它适用于我的情况下的additional()