Saturday, June 29, 2019

linking three models??

Hey guys,

I've successfully linked two models and printed that information out into an html template, but I can't seem to get three models linked.  

Any help would be appreciated:

class Course(models.Model):  
Course_Teacher = models.ManyToManyField(Teacher, blank=False)
Course_Title = models.CharField(max_length=150, null=False)  

class Student(models.Model):
Student_Last_Name = models.CharField(null=False, max_length=50)
Student_First_Name = models.CharField(null=False, max_length=50)
Course_Enrollment = models.ManyToManyField(Course, blank=True)  

class Teacher(models.Model):
Teacher_Last_Name = models.CharField(max_length=50, null=False)
Teacher_First_Name = models.CharField(max_length=50, null=False)  

class Roster(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    student = models.ForeignKey(Student, on_delete=models.CASCADE)


class RosterFilter(django_filters.FilterSet):
    class Meta:
        model = Roster
        fields = {
            'teacher__Teacher_First_Name': ['contains'],
            'teacher__Teacher_Last_Name': ['contains'],
            'course__Course_Title': ['contains'],
            'student__Epicenter_ID': ['contains'],
            'student__Student_First_Name': ['contains'],
            'student__Student_Last_Name': ['contains'],


def teacher_roster_view(request):
    qs = Roster.objects.prefetch_related('Course_Enrollment', 'Course_Teacher')
    roster_list = RosterFilter(request.GET, queryset=qs)
    #list out objects, this is a search viewp
    template_name = 'roster_view.html'
    context = {'object_list': roster_list}
    return render (request, template_name, context)


{% extends "base.html" %}

{% block content %}
<h3 align="center"> Scroll Down to View All or Filter </h3>
<form method="get">
    {{ object_list.form.as_p }}
    <button type="submit">Search</button>

<h1 align="center"> Course Roster </h1>
{% for object in object_list.qs%}
{% include 'roster-inline.html'%}
{% endfor %}
{% endblock %}

#in-line template:

<div class='col-80 col-md-100 mb-1 mx-auto'>
  <div class='card'>
    <div class='card-body'>
      <h5 class = 'card-title'> {% for teacher in object.teacher.all %} {{self.teacher.Teacher_First_Name}} {{self.teacher.Teacher_Last_Name}}<p></p>{% endfor %}</h5>
      <h5 class = 'card-title'> {% for student in object.student.all %} {{self.student.Student_First_Name}} {{self.student.Student_Last_Name}}<p></p>{% endfor %}</h5> 
      <h5 class = 'card-title'> {% for course in object.course.all %} {{self.course.Course_Title}} <p></p>{% endfor %}</h5> 

Charlotte Wood, MEd


(405) 578-5701

Zoom Meeting ID#: 4055785701

Zoom URL:

Classroom Google Site:

Epic Technical Support: (405) 652-0935

Jordan McKesson Principal

405-749-4550 ext. 309

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment