Saturday, June 29, 2019

Re: linking three models??

I am not sure I understand what you wish to do. What output would you need?

An unrelated tip: in Python the convention is to use lowercase names for variables and functions (words separated by underscores).

It makes the code more readable to the vast majority of Python developers, or at least those who adhere to the PEP8 styling guide.


Kind regards,
Lloyd


Sent with Shift

On Sat, Jun 29, 2019 at 5:43 PM Charlotte Wood <charlotte.wood@epiccharterschools.org> wrote:
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)  

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)


# 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'],
        }


# 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)

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



#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> 
      <p></p>
    </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

jordan.mckesson@epiccharterschools.org



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

--
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/CAH-SnCDFmDW9qOHSU2qyNoBzW%2BfNmHzgNP7wxdqXue-gkDrMYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment