Saving the responses

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 ...

Get Django Project Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.