Django Form Wizard to Edit Model

前端 未结 5 550
攒了一身酷
攒了一身酷 2021-01-31 22:47

I have a Django form wizard working nicely for creating content of one of my models. I want to use the same Wizard for editing data of existing content but can\'t find a good ex

5条回答
  •  Happy的楠姐
    2021-01-31 23:29

    I had to edit this a bit to get it working in Django 1.11 with django-formtools 2.1.

    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_dict:
                if 'project_id' in self.kwargs:
                    project_id = self.kwargs['project_id']
                    return Project.objects.get(id=project_id)
            return None
    

    The get_form_instance method now expects either an object or None to be returned.

提交回复
热议问题