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
An update that works with Django 3.1 and django-formtools 2.2. In my case i had two models (Colaborattor and User), connected through OneToOneField. I show the user form in step 0 and colaborattor form in step 1:
models.py:
class Colaborattor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nome = models.CharField("Nome completo", max_length=128)
...
views.py:
class UpdateColaborattorWizard(SessionWizardView):
form_list = [forms.UserRegisterForm, forms.ColaborattorForm]
template_name = "criar_colaborador.html"
def get_form_instance(self, step):
# step 0: user form
if 'colaborattor_pk' in self.kwargs and step == '0':
colaborattor_pk = self.kwargs['colaborattor_pk']
colaborattor = models.Colaborattor.objects.get(pk=colaborattor_pk)
user = colaborattor.user
return user
# step 1: colaborattor form
elif 'colaborattor_pk' in self.kwargs and step == '1':
colaborattor_pk = self.kwargs['colaborattor_pk']
colaborattor = models.Colaborattor.objects.get(pk=colaborattor_pk)
return colaborattor
# The default implementation
return self.instance_dict.get(step, None)
I hope it helps someone with the same problem.