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:
#models.py
class Course(models.Model):
Course_Teacher = models.ManyToManyField(Teacher, blank=False)
Course_Title = models.CharField(max_length=150, null=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)
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)
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)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
# filters.py
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'],
}
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'],
}
# views.py
@login_required
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)
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)
template:
{% 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>
</form>
<h1 align="center"> Course Roster </h1>
{% for object in object_list.qs%}
{% include 'roster-inline.html'%}
{% endfor %}
{% endblock %}
{% 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>
</form>
<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>
<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>
<p></p>
</div>
</div>
</div>
</div>
</div>
</div>
Charlotte Wood, MEd Educator (405) 578-5701 Zoom Meeting ID#: 4055785701 Zoom URL: https://epiccharterschools.zoom.us/j/2970513912 Classroom Google Site: https://sites.google.com/epiccharterschools.org/charlottewoodclassroom/home 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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPZR0N4s8MPQBoVMw_Fy%2BF5TBbz3s2vGZfi6NHU%3DSG1vG36T6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment