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