2
I am trying to put in a view the amount of students enrolled in a course using the following Models below.
NOTE: I cannot change the columns of the Models, since the database already exists and I am just making another system from it.
But how does it work? So:
1 course (Mdlcourse) has N types of registration (Mdlenrol).
N types of registration have N registrations. (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 registration type ID. And the registration type table has the course ID.
That is, I need to walk two tables to have the number of students enrolled. This using Django’s relationship methods because he didn’t want to make a RawSQL
.
What I got so far:
{{ course.mdlenrol_set.mdluserenrolments_set.count }}
This is bringing emptiness. When I just do the code below, it brings the amount of enrolment types that the course has. So I believe I’m getting closer.
{{ course.mdlenrol_set.count }}
And how would I get the name of the course that’s on the other table so?
– Diego Souza
Which field represents the course name?
– Hugo Brilhante
It didn’t really show up on my question, but it’s on Model Mdlcourse.
– Diego Souza
If the field is name then you just do Course.name being Course an instance of Mdlcouse.
– Hugo Brilhante
It is that I need to go through two tables to do what I want. For example. I did a for going through the Course object and bring all the courses. Right ? Now I want to know how many enrolled in the course. Then I have to walk up to the table of Mdluserenrolments..
– Diego Souza
Because of that I can’t get where I want.
– Diego Souza
So, the way I did, you get the information you want. But I realized that you want this to be in the Heart variable in context if that’s what you can add dynamically. I’ll give you an example in response.
– Hugo Brilhante
Very good Hugo. But and within the template I can’t do it directly?
– Diego Souza
Let’s go continue this discussion in chat.
– Hugo Brilhante
Strange, this relationship didn’t work.
– Diego Souza
It worked now. I need to make a little adjustment. Thank you!
– Diego Souza