Qazaaleh Taheri
Qazaaleh Taheri

Reputation: 3

How to structure a Django project for a multi-questionnaire system and import questions in bulk?

I'm building a Django project that will host dozens of questionnaires. I have a few architectural and implementation questions:

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

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

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

Answers (1)

PH Huang
PH Huang

Reputation: 49

(1) This is my rule of thumb to split app

  1. It makes project easier to understand for one another
  2. Models in app are oftenly queried/represent a core feature altogether

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

  1. inheritance (note that this take create multiple tables in DB)
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

  1. proxy with manager:

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

Related Questions