django get_available_name() got an unexpected keyword argument 'max_length'
Asked Answered
C

2

7

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

Canaigre answered 6/6, 2017 at 11:42 Comment(0)
C
13

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):
Carlsbad answered 6/6, 2017 at 11:46 Comment(0)
B
0

Change

def get_available_name(self, name):

to

def get_available_name(self, name, max_length=None):
Baboon answered 6/6, 2017 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.