Hi, i need some help modelling an app i’m developing for registering and analyzing class by class attendance for an organization i contribute to.
I want to register the attendance through admin, and have 2 models: Classes and Students, which are related by a ManytoManyField with a through class named Attendance (because i need a field that represents the attendance status: attended, not attended, suspended class, etc.). I need an editable table in admin that basically has this structure:
| Class 1 | Class 2 | ... | Class N
| std1 | status1_1 | status1_2| ... | status1_N
…
| stdM | statusM_1 | statusM_2 | ... | statusM_N
I think i might be able to do something with list_display and a custom method, but i don’t really know how i would go on about this, or even if what i’m using helps me with what i need.
Here is my code simplified (has some spanish in it but i hope the idea comes through):
# Create your models here.
class ScheduledClass(models.Model):
#...
date = models.DateField("Fecha")
class_id = models.IntegerField("Número de clase")
def __str__(self):
return f"Clase {self.id}, Sección {self.course}"
class Student(models.Model):
name = models.CharField("Nombres", max_length=100)
last_name = models.CharField("Apellidos", max_length=100,)
#...
attendance_data = models.ManyToManyField(ScheduledClass, through="Attendance")
def __str__(self):
return f"{self.name} {self.last_name}"
class Attendance(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
attended_class = models.ForeignKey(ScheduledClass, on_delete=models.CASCADE)
class StatusChoices(models.IntegerChoices):
AUSENTE = 0, _("Ausente")
PRESENTE = 1, _("Presente")
SIN_LISTA = 2, _("Sin lista")
CLASE_SUSPENDIDA = 3, _("Clase suspendida")
ASAMBLEA = 4, _("Suspensión por Asamblea")
#Field that indicates attendance status for a specific student to a specific class
status = models.IntegerField("Asistencia", choices=StatusChoices.choices, default=0)
def __str__(self):
return f"{self.attended_class}, {self.student}"