1

I am using FormWizard to complete a set of operation in my app, I have two models Employee and Person, Employee class inherits Person, and all the fields of Person are available for Employee object.

Now I am creating a set of forms using FormWizard, I just wanted to know that. If a user starts entering the data in the forms and fills upto 2 forms out of 4 and is willing to fill the rest of the forms afterwards. So is this possible that the data for the two forms which he filled can be saved in the database. And the next time he comes can complete the operation form the 3rd form.

If anyone knows that then plz help me out, it would be a great help. Thank You!

Prateek
  • 1,538
  • 13
  • 22

1 Answers1

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
  • 11,757
  • 4
  • 41
  • 57
  • can u help me by writing the process_step function and d view, just a bit of code.............i'll b thankfull – Prateek Dec 06 '10 at 13:11
  • Thanks dear for ur answer it is helpful, but after searching i got an app in github named django-merlin which is a better option than using form-wizard. – Prateek Dec 07 '10 at 07:55
  • You should use models.BinaryField() for the postdata attribute - the binary data produced by pickle can't be directly encoded into a TextField – Conor Svensson Aug 19 '15 at 11:20