covid
covid

Reputation: 69

Storing multiple strings to a single field in Django model

I am trying to create a simple test program for a site. Basically the user will enter in a large amount of JSON. Then I will parse the JSON and get 10 random properties from the JSON. Then I will display those 10 properties and ask the user what the answer is. Then I am going to store the users answer in the admin panel as a way for me to check if they were right or wrong.

Basically I want to keep track of

property      user_answer      correct_answer

But I would like to keep it as a single item.

Upvotes: 2

Views: 1178

Answers (1)

Stevy
Stevy

Reputation: 3387

If you are using PostgreSQL you can store multiple strings in an ArrayField and thus keep them as a single item like this:

from django.contrib.postgres.fields import ArrayField
from django.db import models


class YourModel(models.Model):
    answers = ArrayField(models.CharField(max_length=255))
    # other fields for your model

If however, you are not using PostgreSQL as your database, you have to store the data in different fields like this:

from django.db import models


class YourModel(models.Model):
    user_answer = models.CharField()
    correct_answer = models.CharField()
    # other fields for your model

Upvotes: 3

Related Questions