0

I'm making an Learning Management System. There's this model called Homework and User model which is basically a model for students.

class User(AbstractUser):
    # some code

class Homework(models.Model):
    # some code ...
    students_done = models.ManyToManyField(User, blank=True, related_name='homeworks_done')

I wanted to keep track of students that have done a specific homework and I did it by adding students_done field. Now I want the teacher to have the option to give each student a score for a done homework. How can I save the score in the database? I just can't figure out what field to use and how to use it.

ParsaAi
  • 293
  • 1
  • 14

1 Answers1

1

You want a through model:

class User(AbstractUser):
    # some code

class Homework(models.Model):
    # some code ...
    students_done = models.ManyToManyField(..., through="UserHomework")

class UserHomework(models.Model):
   user = models.ForeignKey(User, ...)
   homework = models.ForeignKey(Homework, ...)
   number = models.IntegerField(...)
   ...
Daniel
  • 3,228
  • 1
  • 7
  • 23