Search code examples
pythondjangodjango-formsformwizard

How to create Django FormWizard for one Model?


I have Django Model with many fields which user must fill. If I'll create one ModelForm for this Model it will be big enough for one form. I want to split it using FormWizard. I think it's possible first to create forms dynamically and then create FormWizard using them.

Is this good approach or is there any better way?


Solution

  • To me it seems fine.

    The approach for creating partial forms is written in the docs.

    In short:

    class PartialAuthorForm(ModelForm):
        class Meta:
            model = Author
            fields = ('name', 'title')
    
    class PartialAuthorForm(ModelForm):
        class Meta:
            model = Author
            exclude = ('birth_date',)
    

    Dynamic way of doing this would be:

    def gimme_my_form(field_tuple):
        class MyForm(ModelForm):
            class Meta:
                model = MyModel
                fields = field_tuple
        return MyForm
    

    Eventually you can also parametrize the model this way.