Search code examples
djangodjango-formsdjango-formwizard

Django FormWizard prefill on edit


I'm using a FormWizard to edit objects and I want to be able to prefill the fields from database when the edit page is loaded.

For example if my URL is 'category_edit_wizard/5', the field 'title' should have the value of the category with the ID 5 when the page is loaded.

Any way to do that?

Thank you


Solution

  • If you're using django-formwizard or the new formwizard implementation introduced in Django 1.4 you can use ModelForm's as a step form and pass an instance_dict when creating the WizardView instance.

    instance_dict works the same way initial_dict works - see https://django-formtools.readthedocs.io/en/latest/wizard.html#formtools.wizard.views.WizardView.initial_dict

    If you're using the old formwizard implementation from Django 1.3 and lower, you can pass initial as described here: https://django-formtools.readthedocs.io/en/latest/wizard.html#providing-initial-data-for-the-forms