HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondjangoMinor

Improving models for a Django quiz

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
djangoquizformodelsimproving

Problem

I'm working on a little django quiz taking app. Currently I'm making it's models. So far I've got Quiz, Question, Answer, and Response designed as follows:

```
from django.db import models
from npage.mixins import TranslatedModelMixin
from django.contrib.auth.models import User

# Create your models here.

class Quiz(models.Model):

title = models.CharField(max_length=60,blank=False,)

# show_feedback = models.BooleanField(blank=False,default=True,help_text="Correct answer is shown after question.",)

class Meta:
verbose_name = "Quiz"
verbose_name_plural = "Quizzes"

def __unicode__(self):
return self.title

class Question(models.Model, TranslatedModelMixin):

quiz = models.ManyToManyField(Quiz, blank=True, )

content = models.CharField(max_length=1000, blank=False, help_text="Enter the question text that you want displayed",verbose_name='Question',)
es_content = models.CharField(max_length=1000, blank=True, help_text="Enter the question text that you want displayed",verbose_name='Question',)

type = models.CharField(max_length=8, choices=TYPES, default='radio')
is_scoreable = models.BooleanField(default=True, help_text="Will the answer to this question be scored?")

# idea
# tags = models.CharField(max_length=1000, blank=False, help_text="Enter some text to help you find this question to add it to quizzes later on.",verbose_name='Question',)

feedback = models.TextField(max_length=2000,blank=True,help_text="Explanation to be shown after the question has been answered.",verbose_name='Explanation',)
es_feedback = models.TextField(max_length=2000,blank=True,help_text="Explanation to be shown after the question has been answered.",verbose_name='Explanation',)

TYPES = (
('radio', 'radio'),
('checkbox', 'checkbox'),
('text', 'text'),
)

language_code = 'en'
translated_fields = ['content', 'feedback']

class Meta:
verbose_name = "Questi

Solution

You could have an abstract superclass Response which has concrete derived classes of multipleChoiceResponse and freeResponse. Then you can have a method such as checkResponse which is different for each type of respones.

EDIT:

How about this:

from django.db import models

class Response(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)

    class Meta:
        abstract = True

class freeResponse(Response):
    free_response = models.TextField(max_length=2000,blank=True)

class multipleChoiceResponse(Response) #does not handle multi-select (tickbox) cases
    answer = models.ForeignKey(Answer)

Code Snippets

from django.db import models

class Response(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)


    class Meta:
        abstract = True

class freeResponse(Response):
    free_response = models.TextField(max_length=2000,blank=True)

class multipleChoiceResponse(Response) #does not handle multi-select (tickbox) cases
    answer = models.ForeignKey(Answer)

Context

StackExchange Code Review Q#54522, answer score: 2

Revisions (0)

No revisions yet.