Reputation: 3
I'm building a Django project that will host dozens of questionnaires. I have a few architectural and implementation questions:
Should I create a separate app for questionnaires and another app for test takers within the main Django projectand put models like User _Answer and Test_Result within the test takers app itself?
In the test model, how can I design the database structure to handle different question types? Some questions are only text-based, some are only image-based, and some contain both text and images.
Since each test may contain hundreds of questions, manually entering them via the Django admin panel is impractical. Is there a way to bulk import questions from a Word document?
Upvotes: -2
Views: 46
Reputation: 49
(1) This is my rule of thumb to split app
As long as app delimit make sense to you, it is totally fine.
(2) I'd say have choices of model inheritance and proxy model here
class Question(models.Model):
class QuestionType(models.IntegerChoices):
TEXT = 1
IMAGE = 2
MIXED = 3
# fields that shared between all questions
question_type = models.IntergerField(choices=QuestionType)
class TextQuestion(Question):
description = models.TextField()
# fields that belongs to TextQuestion here
class ImageQuestion(Question):
image = models.ImageField()
# fields that belongs to ImageQuestion here
class MixedQuestion(Question):
image = models.ImageField()
description = models.TextField()
# fields that belongs to MixedQuestion here
share one table in database, proxy makes it easy to use/understand in app
from django.db.models import Manager
class QuestionManager(Manager):
def __init__(self, *args, question_type=None, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.question_type = question_type
def get_queryset(self):
qs = super().get_queryset()
if self.question_type is not None:
qs = qs.filter(question_type=self.question_type)
return qs
class Question(models.Model):
class QuestionType(models.IntegerChoices):
TEXT = 1
IMAGE = 2
MIXED = 3
# fields that shared between all questions
question_type = models.IntergerField(choices=QuestionType)
description = models.TextField()
image = models.ImageField()
class TextQuestion(Question):
question_type = Question.QuestionType.TEXT.value
objects = QuestionManager(question_type=question_type)
class Meta:
proxy = True
class ImageQuestion(Question):
question_type = Question.QuestionType.IMAGE.value
objects = QuestionManager(question_type=question_type)
class Meta:
proxy = True
class MixedQuestion(Question):
question_type = Question.QuestionType.MIXED.value
objects = QuestionManager(question_type=question_type)
class Meta:
proxy = True
(3) Try django shell, read your input and bulk insert using Django ORM
Upvotes: -1