Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django get_available_name() got an unexpected keyword argument 'max_length'

I want to override files with the my django Model. So if I upload 'one' and later upload 'two', 'two' should override 'one' (on the file system). But I get an error.

This is my model:

class StudentAssignment(models.Model):
    file0 = models.FileField(upload_to=use_solution_path, storage=OverwriteStorage(), validators=[validate_file_extension])

This is the storage.

import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage


class OverwriteStorage(FileSystemStorage):
    def get_available_name(self, name):
        """
        Returns a filename that's free on the target storage system, and
        available for new content to be written to.
        """
        # If the filename already exists, remove it as if it was a true file system
        if self.exists(name):
            os.remove(os.path.join(settings.MEDIA_ROOT, name))
        return name

The Error:

  [...]
  File "/home/mb/.local/lib/python3.5/site-packages/django/db/models/fields/files.py", line 95, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "/home/mb/.local/lib/python3.5/site-packages/django/core/files/storage.py", line 53, in save
name = self.get_available_name(name, max_length=max_length)
TypeError: get_available_name() got an unexpected keyword argument 'max_length'

I am new to django and have no idea how to go on. Can someone help? Thanks :)

like image 753
Max Avatar asked Jun 06 '17 11:06

Max


1 Answers

The documentation for file storage classes clearly shows that get_available_name takes a max_length keyword argument. So you need to at least accept that argument, even if you don't use it.

def get_available_name(self, name, max_length=None):
like image 69
Daniel Roseman Avatar answered Nov 18 '22 18:11

Daniel Roseman