[Answered ]-Using FormWizard and saving the forms data in between before the completion of the whole process?

2👍

what you can do is every step, save out the form state to some serialised object in db ForeignKeyed to the user.

then when hooking up the formwizard, wrap the formwizard view in a custom view which checks if the user has a saved form and if so deserialises and redirects to the appropriate step.

Edit: seems formwizard saves state in POST. only need to save postdata.

models.py:

class SavedForm(Model):
    user = ForeignKey(User)
    postdata = TextField()

views.py:

import pickle
class MyWizard(FormWizard):
    def done(self, request, form_list):
        SavedForm.objects.get(user=request.user).delete() # clear state!!
        return render_to_response('done.html',)

formwizard = MyWizard([Form1, Form2]) <- class name, not instance name

def formwizard_proxy(request, step):
    if not request.POST: #if first visit, get stored data
        try:
            prev_data = SavedForm.objects.get(user=request.user)
            request.POST = pickle.loads(prev_data.postdata)
        except:
            pass

    else: # otherwise save statet:
        try:
            data = SavedForm.objects.get(user=request.user)
        except:
            data = SavedForm(user=request.user)
        data.postdata=pickle.dumps(request.POST)
        data.save()

    return formwizard(request)

edit: changed formwizard constructor

👤Thomas

Leave a comment