Django Form Wizard to Edit Model

前端 未结 5 503
攒了一身酷
攒了一身酷 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条回答
  •  长情又很酷
    2021-01-31 23:46

    Addition to pxg's answer, get_form_instance should be like this, otherwise you won't be editing the model but create a new instance of it:

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

提交回复
热议问题