I have a Django form wizard that works great for creating the contents of one of my models. I want to use the same Wizard to edit the data of existing content, but I can not find a good example of how to do this.
Here is a simplified version of the project code:
forms.py
class ProjectEssentialsForm(forms.ModelForm): class Meta: model = Project fields = [ 'title', 'short_description', 'who_description', 'problem_description', 'solution_description' ] class ProjectYourInfoForm(forms.ModelForm): class Meta: model = Project fields = [ 'gender', 'location', 'post_code', 'sector', ]
views.py
TEMPLATES = { 'project_essentials': 'projects/essentials-form.html', 'project_your_info': 'projects/your-info-form.html', } class ProjectWizard(SessionWizardView): instance = None def get_form_instance(self, step): """ Provides us with an instance of the Project Model to save on completion """ if self.instance is None: self.instance = Project() return self.instance def done(self, form_list, **kwargs): """ Save info to the DB """ project = self.instance project.save() def get_template_names(self): """ Custom templates for the different steps """ return [TEMPLATES[self.steps.current]]
urls.py
FORMS = [ ('project_essentials', ProjectEssentialsForm), ('project_your_info', ProjectYourInfoForm), ] urlpatterns = patterns('', (r'^projects/add$', ProjectWizard.as_view(FORMS)), )
I see that this function exists https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#django.contrib.formtools.wizard.views.WizardView.get_form_instance to set the instance of the form, but I'm not sure how you are going to get the model identifier, look here and see exactly how the code will work.
Sample code or a link to it would be appreciated.
Thanks Pete