Django Form Wizard to Edit Model

I've just got this working so will post the answer in case it helps someone else.

You can pass the ID of the item you'd like to edit in urls.py like this:

(r'^projects/edit/(?P<project_id>[-\d]+)$', ProjectWizard.as_view(FORMS)),

You can then look up the item with following code in

views.py:

class ProjectWizard(SessionWizardView):
    def get_form_initial(self, step):
        if 'project_id' in self.kwargs and step == 'project_essentials':
            project_id = self.kwargs['project_id']
            project = Project.objects.get(id=project_id)
            from django.forms.models import model_to_dict
            project_dict = model_to_dict(project)
            return project_dict
        else:
            return self.initial_dict.get(step, {})

You need to convert the model to a dict so you can set it as the initial data.


pxg's answer is insufficient. As pointed out by emin-buğra-saral it creates a new instance of the model rather than editing it. And emin-buğra-saral's answer, by itself, isn't enough. Either don't override the get_form_initial method and don't set an initial_dict value or use the implementation provided in this answer. This is how you should combine their answers:

in urls.py:

(r'^projects/edit/(?P<project_id>[-\d]+)$', ProjectWizard.as_view(FORMS)),

in views.py:

class ProjectWizard(SessionWizardView):
    def get_form_initial(self, step):
        if 'project_id' in self.kwargs:
            return {}
        return self.initial_dict.get(step, {})

    def get_form_instance(self, step):
        if not self.instance:
            if 'project_id' in self.kwargs:
                project_id = self.kwargs['project_id']
                self.instance = Project.objects.get(id=project_id)
            else:
                self.instance = Project()
        return self.instance

While pxg's version of get_form_initial would actually work (as long as you also add the get_form_instance override) it's not necessary to look up the instance, extract its data, and create an initial value dictionary. All this is done automatically by the ModelForm prior to initializing the instance from initial_dict. By simply returning an empty initial value dictionary you'll have simpler, more efficient code.

Tags:

Python

Django