Search code examples
djangoformsmodelmulti-step

Django: One model multi-steps forms


I am trying to build a multi-step forms from one Django model which have 9 fields. The forms are validated in each step and saved after final step into database.

The quick solution I can think of beside Django formWizard is

I have a django model like this:

class Question(models.Model):
    name = models.CharField(max_length=100)
    Description = models.TextField()
    filter = models.CharField(max_length=20)

I split the form into three

class Filter(forms.ModelForm):
    class Meta:
        model = Question
        exclude = ('filter',)


class Name(forms.ModelForm):
    class Meta:
        model = Question
        fields = ('name',)


class Descripton(forms.ModelForm):
    class Meta:
        model = Question
        fields = ('description',)

Write urls and integrate with views for different steps. The question is how can I validate and save multi-steps forms data at last step?


Solution

  • What is your use case for splitting this up into three steps rather than in a single one?

    That aside, a simple solution would be to:

    1. Clean/validate the partial data after each submit.
    2. Send the partial data back in hidden fields to your template for the subsequent steps (perhaps Name can inherit or be initialized using Filter, same with Description taking in Name).
    3. Then on the final submit, you'll have all the previous data (some in hidden fields) so you can save it all to your database.