1

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?

1
  • It would be better to use the default id field as Result's primary key. Naming could be better as well; Questions should be Question (since one record is one question) and having a field result in Result is confusing, but I can't suggest something better since I have no idea what marks and result represent. Jul 7, 2011 at 21:37

2 Answers 2

6

You should use one-to-many (ForeignKey) relationships instead of many-to-many:

class Result(models.Model):
     user = models.ForeignKey(User)
     paper = models.ForeignKey(ExamPaper)
     question = models.ForeignKey(Question)
     marks = models.IntegerField()
     result = models.CharField()

     class Meta:
         unique_together = (("user", "paper", "question"), )

If a question can appear only on one exam paper, then Question.paper should also be a ForeignKey and you could remove the paper field from Result.

4
  • Also, you probably don't need the paper field either, because you can access that by doing result.question.paper, unless of course the paper field in the Result will not be the same for the paper field in Question related to the Result, if that makes sense. Jul 7, 2011 at 21:37
  • 1
    @Bryce: Can't do that because result.question.paper is a many-to-many relationship. I.e. a question can appear in multiple papers. I think this is for a system where exams contain a set of questions randomly chosen from a pool, so each student's exam is different, discouraging cheating. Jul 7, 2011 at 21:41
  • I got exactly the db schema I was looking for by changing manytomany fields to foreignkey fields. Thanks. Actually question can appear in multiple papers and hence I need user, paper and question unique combined together. Thanks again.
    – Shwetanka
    Jul 7, 2011 at 21:42
  • Ah, missed that. Maybe I should have downvoted, then, since that makes this a wrong answer, per OP's comment? Jul 8, 2011 at 2:54
0

To begin with, you probably want the unique_together option for the _meta class. See: https://docs.djangoproject.com/en/1.3/ref/models/options/#unique-together

EDIT: Looks like django requires at least one primary key field. https://docs.djangoproject.com/en/1.3/topics/db/models/#automatic-primary-key-fields

2
  • but it doesn't allow to use unique_together with ManyToManyFields
    – Shwetanka
    Jul 7, 2011 at 21:30
  • You shouldn't use ManyToMany in Result. You don't have one result for a set of user, a set of questions, and set of results... and if you do, I think you need to refactor your schema, because things will get very painful when writing queries. Jul 7, 2011 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.