5

I have been struggling to create a django FormWizard. I think that I am pretty close, but I cannot figure out how to save to the database.

I tried the solution suggeted here:

def done(self, form_list, **kwargs):
    instance = MyModel()
    for form in form_list:
        for field, value in form.cleaned_data.iteritems():
            setattr(instance, field, value)
    instance.save()

    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })

but placing it in the done method results in a No Exception Supplied error. Placing this code in the save method, on the other hand, does not save the information.

I also tried the solution suggested here:

def done(self, form_list, **kwargs):
    for form in form_list:
        form.save()
    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })

But this returns another error: AttributeError at /wizard/ 'StepOneForm' object has no attribute 'save'. Have you faced this problem? How do I save information to the database after the wizard is submitted? Thanks

4 Answers 4

1
def done(self, form_list, **kwargs):
    new = MyModel()
    for form in form_list:
        new = construct_instance(form, new, form._meta.fields, form._meta.exclude)
    new.save()
    return redirect('/')
Sign up to request clarification or add additional context in comments.

Comments

0

Try This method

def done(self,request,form_list):
    if request.method=='POST':
        form1 = F1articles(request.POST)    
        form2 = F2articles(request.POST)
        form_dict={}
        for x in form_list:
            form_dict=dict(form_dict.items()+x.cleaned_data.items())
        insert_db = Marticles(heading = form_dict['heading'],
        content = form_dict['content'],
        created_by = request.session['user_name'],  
        country = form_dict['country'],
        work = form_dict['work'])
        insert_db.save()
        return HttpResponse('data saved successfully')

Comments

0

you need to get an instance before:

def get_form_instance( self, step ):
    if self.instance is None:
        self.instance = MyModel()
    return self.instance

def done(self, form_list, **kwargs):
    self.instance.save()
    return HttpResponseRedirect(reverse('mymodel_finish'))

Comments

0

This work very fine, even with file fields

    ....
    template_name = "..."
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT,'tmp'))

    def done(self, form_list, **kwargs):
        form_data, form_files = process_data(form_list)        
        form = MyModelForm(data=form_data, files=form_files)
        if form.is_valid():
            obj.save()
            return redirect('some_success_page')

def process_data(form_list):
    """ the function processes and return the field data and field files as a tuple """
    fields = {}
    files = {}
    for form in form_list:
    ## loop over each form object in the form_list list object
        for  field, (clean_field, value) in zip(form, form.cleaned_data.items()):
            ## loop over each field in each form object
            if field.widget_type == 'clearablefile':
                ## if field type == "clearablefile" add to files dict and continue to next iteration
                files.update({clean_field:value})
                continue
            ## else add to the field dict  
            fields.update({clean_field:value})
    return fields, files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.