Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django image.save() TypeError: get_valid_name() missing positional argument: 'name'

This is what I have tried:

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()

On the image.image.save() line, the error trace is:

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'

Is the problem because Django is expecting a local OS path? I'm using S3BotoStorage, the ImageField is using a separate bucket from the rest of the project:

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)
like image 536
davidtgq Avatar asked Dec 04 '15 22:12

davidtgq


1 Answers

Perhaps see Trying to understand Django source code and cause of missing argument TypeError and change

image = models.ImageField(storage=S3CustomStorage)

to

image = models.ImageField(storage=S3CustomStorage())

I had the identical TypeError ;-) but now it works with the additional () in my case

like image 135
andreas.naturwiki Avatar answered Nov 07 '22 18:11

andreas.naturwiki