Mecha
Mecha

Reputation: 79

save() got multiple values for keyword argument 'max_length'

I'm using the save method of django, and I have the error of save() got multiple values for keyword argument 'max_length'

This is my model:

def generate_path(instance, filename):
     section=instance.document_title.historical_set.last().id_section
     year=str(section.year.number)
     course=(section.course.name)
     section=str(section.number)
     curso=curso.encode('utf-8').decode('utf-8')
     return os.path.join(ciclo,curso,seccion,filename)


class UseExistingStorage(FileSystemStorage):

   def save(name, content, max_length=None):
       if not self.exists(name):
           return super().save(self,name, content, max_length)
       return name

class Field(models.Model):
     type=models.CharField(max_length=50, choices=document_type, default=None)
     document_title=models.ForeignKey(Document,on_delete=models.CASCADE,null=True)
     file = models.FileField(null=True,blank=True,     upload_to=generate_path,storage=UseExistingStorage())
     rubric=models.FileField(null=True,blank=True,upload_to=generate_path,storage=UseExistingStorage())

and this is how I save the field:

if FieldForm.is_valid():
  course=request.POST.get('course')
  coursename=Course.objects.values('name').get(name=course)

  try:
       field.file=request.FILES['file']

  except:
        pass
  try:
       field.rubric=request.FILES['rubric']

  except:
       pass
  if type.find('a')!=-1:
       field.type='a'
  elif coursename.find('b')!=-1 :
       field.type='b'
  elif type.find('c')!=-1:
       field.type='c'
  else:
       field.type='d'

  field.document_title=documentTitle
  field.save()

In generate path, I do the path to save the document in year/course/section and in the storage check if the field exist in that location.But I don't knoow why I'm getting that error

Upvotes: 1

Views: 414

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You dont need to pass self explicitly, super() will do it for you, try this:

return super().save(name, content, max_length)

instead of

return super().save(self,name, content, max_length)

Also first argument of overriding save() should be self also:

def save(self, name, content, max_length=None):
   if not self.exists(name):
       return super().save(name, content, max_length)
   return name

UPD

For python2 you should pass self and class as super's arguments:

return super(UseExistingStorage, self).save(name, content, max_length)

Upvotes: 2

Related Questions