Make a Relationship Many for Many

Asked

Viewed 225 times

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 }}

2 answers

2


You can filter students this way:

MdlUserEnrolments.objects.filter(enrolid__courseid=course.id).count()

Say you want to put the filter value in each course of context:

courses = MdlCouses.objects.all()
for course in courses:
    count = MdlUserEnrolments.objects.filter(enrolid__courseid=course.id).count()
    course.count = count

Now in your template is possible to access the amount of students of each course so:

{{course.count}}
  • And how would I get the name of the course that’s on the other table so?

  • Which field represents the course name?

  • It didn’t really show up on my question, but it’s on Model Mdlcourse.

  • If the field is name then you just do Course.name being Course an instance of Mdlcouse.

  • 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..

  • Because of that I can’t get where I want.

  • 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.

  • Very good Hugo. But and within the template I can’t do it directly?

  • Strange, this relationship didn’t work.

  • It worked now. I need to make a little adjustment. Thank you!

Show 6 more comments

2

From a survey on the select_related https://docs.djangoproject.com/en/2.0/ref/models/querysets/

With it you can browse your tables in more optimized ways like:

user = MdlUserEnrolments.objects.select_related('enrolid')

Ai to access the related table fields you can use in the template:

{% for u in user %} Estatos do MdlUserEnrolments: {{ u.status }} Estatos do enrolid: {{ u.enrolid.status }} {% endfor %}

I’ve only done it with 2 tables so far, but if I’m not mistaken you can do something like: u.enrolid.courseid.fullname to print

A get you could do like this user = MdlUserEnrolments.objects.select_related('enrolid').get(enrolid__status=10)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.