I have these models
class User(models.Model):
user_name = models.CharField()
ph_number = models.CharField()
class ExamPaper(models.Model):
paper_name = models.CharField()
paper_subject = models.CharField()
class Questions(models.Model):
paper = models.ManyToManyField(ExamPaper, related_name='question_set')
question_num = models.IntegerField()
question_text = models.TextField()
Now I want to store the results of each question by each user for each paper. Paper will have multiple questions and a question may also belong to multiple papers. User may give multiple papers and multiple questions.
I want mysql table to have user, paper and question to define primary key taken all together and two more fields ‘marks’ and ‘result’. I’m not able to understand how to do this in django models. Will this work:
class Result(models.Model):
user = models.ManyToManyField(User)
paper = models.ManyToManyField(ExamPaper)
question = models.ManyToManyField(Question)
marks = models.IntegerField()
result = models.CharField()
Please anyone can anyone explain?
You should use one-to-many (ForeignKey) relationships instead of many-to-many:
If a question can appear only on one exam paper, then Question.paper should also be a
ForeignKeyand you could remove thepaperfield fromResult.