开发者

Using FormWizard and saving the forms data in between before the completion of the whole process?

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 ju开发者_如何学编程st 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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜