1๐
โ
You will need another model, can be Question
. The final result would be something like:
class User(models.Model):
user_name = models.CharField(...)
class Question(models.Model):
question_text = models.CharField(...)
class UserAnswer(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(User)
answer = models.CharField(...)
If you want more complicated answers, like especific values, lists of values, you can create one more model:
class QuestionAlternative(models.Model):
question = models.ForeignKey(Question)
value = models.CharField(...)
And then redefine UserAnswer
:
class UserAnswer(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(User)
answer = models.ForeignKey(QuestionAlternative)
With this, you will have the Questions
in one place, one UserAnswer
per question, and the QuestionAlternative
s how many times they must exist. Does not worry about the ForeignKey
fields, they are not overheads and they build beautiful, reusable structures.
๐คJean Jung
Source:stackexchange.com