Save () received unexpected keyword argument 'commit' Django error

im geting this error "save () received an unexpected keyword argument 'commit'" what I'm trying to do is ask the user when the user uploads his files.

update, I added my model.py and forms.py, as well as a screenshot of the error, sorry for the time fisrt is learning python / django.

screenshot

model.py

class Document(models.Model):
    fs = FileSystemStorage(location=settings.MEDIA_ROOT)
    input_file  = models.FileField(max_length=255, upload_to='uploads', storage=fs)
    user = models.ForeignKey(User)

    def __unicode__(self):
        return self.input_file.name

    @models.permalink
    def get_absolute_url(self):
        return ('upload-delete', )

forms.py

class BaseForm(FileFormMixin, django_bootstrap3_form.BootstrapForm):
    title = django_bootstrap3_form.CharField()



class MultipleFileExampleForm(BaseForm):
    input_file = MultipleUploadedFileField()

    def save(self):
        for f in self.cleaned_data['input_file']:
            Document.objects.create(
                input_file=f
            )

here is my view.py

@login_required
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = MultipleFileExampleForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = form.save(commit=False)
            newdoc.user = request.user
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myfiles.views.list'))
    else:
        form = MultipleFileExampleForm() # A empty, unbound form

    documents = Document.objects.all

    return render_to_response(
        'example_form.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )
+4
source share
2 answers

You are not a unit of django.forms.ModelForm , but you write your code just like you.

You need a subclass of ModelForm (which has a save method with a commit argument).

, .

commit = False, , django.forms.ModelForm

. save_all_files - . commit = False, . .

, , commit = False ModelForm :

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

+2

, , (.. commit arg). super() , .

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

All Articles