I am in the process of making a webapp, and this webapp needs to have a form wizard. The wizard consists of 3 ModelForms, and it works flawlessly. But I need the second form to be a "edit form". That is, i need it to be a form that is passed an instance.
How can you do this with a form wizard? How do you pass in an instance of a model? I see that the FormWizard class has a get_form method, but isnt there a documented way to use the formwizard for editing/reviewing of data?
In Django 1.4 you can do something like:
def edit(request):
initial = {
0: {'subject': 'Hello', 'sender': 'user@example.com'},
1: {'message': 'Hi there!'}
}
wiz = FormWizard([form1,form2,form3],initial_dict = initial)
return wiz(request)
you can use formwizard class in a view and pass initial there, something like:
def edit(request):
initial = {0: {'field1':'value1'}}
return FormWizard([form, some_other_form], initial=initial)
where initial should be a dict with keys equal to steps and values are dicts of data just like for usual form.
I think there's a few approaches, depending how complicated the data you have to pass is.
You can follow the instructions here: https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#providing-initial-data-for-the-forms
and create an initial dictionary that you pass to the view in urls.py like so:
>>> initial = {
... '0': {'subject': 'Hello', 'sender': 'user@example.com'},
... '1': {'message': 'Hi there!'}
... }
>>> wiz = ContactWizard.as_view([ContactForm1, ContactForm2], initial_dict=initial)
Your other option, and this is more complex, but will allow you to have a little more logic, is to override get_initkwargs and put the logic in there (see Django code: https://github.com/django/django/blob/master/django/contrib/formtools/wizard/views.py).
Finally, if you need to provide the object based on the previous form's input, then it's going to get quite complicated, because get_initkwargs is a class method and initial dictionaries need to be passed when the wizard is initiated. But, you can probably do it by overriding get_form_kwargs:
def get_form_kwargs(self, step=None):
kwargs = {}
if step != '0':
your_field = self.get_cleaned_data_for_step('0')['your_field']
# logic for getting object based on field goes here
kwargs.update({'object': object,})
return kwargs
Then you could use the form's init method to set initial values based on the object you passed in kwargs. I use that last piece of code a lot, but haven't really used the earlier ones.