11from django .db import models
2- from django .contrib .auth .models import User
3- from django .utils import timezone
2+
3+ # Create your models here.
4+ from django .contrib .auth .models import User # Inbuilt auth User model
45
56class Exam (models .Model ):
67 title = models .CharField (max_length = 255 )
7- duration = models .DurationField ()
8+ duration = models .DurationField ()
89 course_name = models .CharField (max_length = 255 )
910 metadata = models .TextField (blank = True )
1011 created_at = models .DateTimeField (auto_now_add = True )
@@ -13,18 +14,23 @@ def __str__(self):
1314 return self .title
1415
1516class Question (models .Model ):
17+ # Defining enums/choices
1618 QUESTION_TYPES = [
1719 ('MCQ' , 'Multiple Choice' ),
1820 ('SA' , 'Short Answer' ),
1921 ]
2022
21- exam = models .ForeignKey (Exam , related_name = 'questions' , on_delete = models .CASCADE )
23+ exam : Exam = models .ForeignKey (Exam , related_name = 'questions' , on_delete = models .CASCADE )
2224 question_text = models .TextField ()
23- question_type = models .CharField (max_length = 3 , choices = QUESTION_TYPES , default = 'MCQ' )
25+ question_type = models .CharField (
26+ max_length = 3 , choices = QUESTION_TYPES ,
27+ default = 'MCQ'
28+ ) # using choices
2429
25- options = models .JSONField (default = dict , blank = True , help_text = "For MCQs: {'options': ['A', 'B', 'C']}" )
26- correct_answers = models .JSONField (help_text = "e.g. {'answer': 'B'}" )
27-
30+ # Flexible storage for options (e.g. ["A", "B", "C"]) and correct answers
31+ options = models .JSONField (
32+ default = dict , blank = True , help_text = "For MCQs: {'options': ['A', 'B', 'C']}" )
33+ correct_answers = models .JSONField (default = dict , help_text = "e.g. {'answer': 'B'}" )
2834 order = models .PositiveSmallIntegerField (default = 1 )
2935
3036 class Meta :
@@ -39,10 +45,12 @@ class Submission(models.Model):
3945 ('graded' , 'Graded' ),
4046 ]
4147
48+ # Link the submission to the inbuilt User model via FK
4249 student = models .ForeignKey (User , on_delete = models .CASCADE )
4350 exam = models .ForeignKey (Exam , on_delete = models .CASCADE )
4451 submitted_at = models .DateTimeField (auto_now_add = True )
4552
53+ # Allow score and feedback to be blank (nullable) initially until graded
4654 total_score = models .DecimalField (max_digits = 5 , decimal_places = 2 , null = True , blank = True )
4755 feedback = models .TextField (blank = True )
4856 status = models .CharField (max_length = 10 , choices = STATUS_CHOICES , default = 'pending' )
@@ -55,6 +63,7 @@ class Answer(models.Model):
5563 question = models .ForeignKey (Question , on_delete = models .CASCADE )
5664 student_answer = models .JSONField ()
5765
66+ # Optional: store individual score per question if needed later
5867 is_correct = models .BooleanField (default = False , blank = True )
5968
6069 def __str__ (self ):
0 commit comments