We want to save the response of the user filling in one of our dynamic forms. As the data from a dynamic form will also be dynamic, we need to store an unknown number of fields and their values in our database. As we have already seen, JSON is a reasonable way to store such data. Let's create a new model in main/models.py
to store the responses:
class FormResponse(models.Model): form = models.ForeignKey(FormSchema) response = JSONField()
Next, create and run the migrations to add this new model to our database:
> python manage.py makemigrations main Migrations for 'main': 0002_formresponse.py: - Create model FormResponse > python manage.py migrate main Operations to perform: Apply all migrations: main Running migrations: Rendering model states... DONE Applying main.0002_formresponse... OK
Now that we have our model, we need to save valid responses for our forms in this model. The form_valid
method of CustomFormView
is the correct place to add this...