I'm trying to put in a view the number of students enrolled in a course using the following Models below.
I can not change the columns of the Models, since the database already exists and I'm just making another system from it.
But how does it work? So:
1 course (MdlCourse) has N enrollment types (MdlEnrol).
N enrollment types have N enrollments. (MdlUserEnrolments)
class MdlCourse(models.Model):
id = models.BigAutoField(primary_key=True)
fullname = models.CharField(max_length=200)
class MdlUserEnrolments(models.Model):
id = models.BigAutoField(primary_key=True)
status = models.BigIntegerField()
enrolid = models.ForeignKey(MdlEnrol, on_delete = False, db_column = 'enrolid')
class MdlEnrol(models.Model):
id = models.BigAutoField(primary_key=True)
enrol = models.CharField(max_length=20)
status = models.BigIntegerField()
courseid = models.ForeignKey(MdlCourse, on_delete = False, db_column = 'courseid')
The Model MdlUserEnrolments does not have the Course ID. It has the subscription type ID. And the enrollment type table has the course ID.
That is, I need to walk two tables to have the number of students enrolled. This is using Django's relationship methods, since I did not want to make a RawSQL
.
What I've achieved so far:
{{ course.mdlenrol_set.mdluserenrolments_set.count }}
This is bringing empty. When I just make the code below, it brings up the amount of enrollment types that the course has. So I think I'm getting close.
{{ course.mdlenrol_set.count }}