Search code examples
djangoformwizard

Django, DataWizard, SessionWizardView get current user


I am creating a series of forms, processed via Django's form wizard, however, the forms consist of many dropdown boxes, the content of which depend on the current user and therefore, I need to pass the User as a kewword argument.

What I have at the moment is this:

class ViewDataWizard(SessionWizardView):
    template_name="wizards/data/view_data.html"
    def done(self, form_list,**kwargs):
        form_data = process_form_data(form_list)
        return render_to_response('wizards/data/view_data_done.html',{'form_data':form_data})

However, I am seeking to produce this:

class ViewDataWizard(SessionWizardView):
    template_name="wizards/data/view_data.html"
    def get_form_kwargs(self, step):
        if step == 0:
            return {'user':<USEROBJECT>}
        else:
            return {}
    def done(self, form_list,**kwargs):
        form_data = process_form_data(form_list)
        return render_to_response('wizards/data/view_data_done.html',{'form_data':form_data})

Where in the second example above, I need to substitute USEROBJECT with the current user, to seed the series of forms.

Am I missing something really obvious? Traditionally I would get user from request.user in a given view, however, this seems elusive in the forms wizard process...


Solution

  • You can get the request object from self.

    def get_form_kwargs(self, step):
        if step == 0:
            return {'user': self.request.user}
        else:
            return {}