0

I know there's a similar questions to this one here. My question builds on that answer.

I am building a web app that will allow a user to create quizzes for educational purposes. This implies that there will be hundreds of questions created.

Option 1

In the quoted answer the method suggested is to use a ForeignKey relationship as such:

class Question(models.Model):
    prompt = models.TextField (max_length=300)
    answer = models.CharField (max_length=50) 

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice = models.CharField (max_length=300, blank=True)

However, the problem with this method is that if I have 300 questions, that will create 1200-1800 or choice entries. As there are more and more questions created, that number will grow by 10. As an educator, I plan to use this for my work (I have several thousand questions) and share the app with other teachers who will all be adding and sharing their own questions. I am concerned that on a larger scale this will create performance issues with the database.

Option 2

What has been suggested to me is to use this approach instead:

class Question(models.Model):
    prompt = models.TextField (max_length=300)
    answer = models.CharField (max_length=50) 

class ChoiceList(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice1 = models.CharField (max_length=300, blank=True)
    choice2 = models.CharField (max_length=300, blank=True)
    choice3 = models.CharField (max_length=300, blank=True)
    choice4 = models.CharField (max_length=300, blank=True)
    choice5 = models.CharField (max_length=300, blank=True)
    choice6 = models.CharField (max_length=300, blank=True)
    choice7 = models.CharField (max_length=300, blank=True)
    choice8 = models.CharField (max_length=300, blank=True)
    choice9 = models.CharField (max_length=300, blank=True)
    choice10 = models.CharField (max_length=300, blank=True)  

Option 3

There is a third alternative here as well, which is to store the answers as in the first example but by using a list [choice1, choice2, choice3...]. That way avoids null values and also would not create 6 average choice entries per question entry.

Are there any issues that I am not foreseeing by creating a single entry of ChoiceList for each Question?

Which approach will be most effective for my app?

  • 2
    The first alternative is obviously better, in fact the answer to the question you link explains it pretty well. Your alternative is simply bad design what if a question doesn't have 10 choices you will have null values that is what. A good database design strives to prevent null values. Your concern about having multiple entries in the table is minor 1200-1800 is quite a small number for a database, a good database (and one that is managed properly) can handle much more rows than that efficiently. – Abdul Aziz Barkat Apr 04 '21 at 13:17
  • Thanks for that answer. I am aware that 1200-1800 is small, but that number will be 12,000-18,000 for me, and for each other instructor. There is a third alternative, which is to use a list, which I'm going to mention in an edit. – Yuri.teacher.English Apr 04 '21 at 13:52
  • 1
    12,000-18,000 is still not a big number for a database. See [How many rows in a database are TOO MANY?](https://stackoverflow.com/questions/1926079/how-many-rows-in-a-database-are-too-many) – Abdul Aziz Barkat Apr 04 '21 at 13:55

0 Answers0