[Solved]-Save() got an unexpected keyword argument 'commit' Django Error

7๐Ÿ‘

โœ…

You are not sub classing django.forms.ModelForm, yet, you are writing your code like you are.

You need to subclass ModelForm (which has the save method with the commit argument).

Calling super will not work either, as the super class has no save method with that argument.

Remove the commit=False it will never work unless you rewrite your code to subclass django.forms.ModelForm

In any case the save method should always return an instance. I suggest you rename your method to save_all_files or something similar. You will not be able to use commit=False to save multiple object in your save method. It is not the intended use.

For further reading, you can read the source to know how the commit=False works in the ModelForm class at the following address :

https://github.com/django/django/blob/master/django/forms/models.py

0๐Ÿ‘

I believe you are completely overriding the save method, which gets rid of the existing functionality (i.e. the commit arg). Try running a super() at the end so that it has the existing save functionality as well.

def save(self):
    for f in self.cleaned_data['input_file']:
        Document.objects.create(
            input_file=f
        )
    super(MultipleFileExampleForm, self).save(*args, **kwargs)
๐Ÿ‘คawwester

Leave a comment