如何在ListView中正确使用get_context_data来获取Django中的相关实例

时间:2018-02-28 10:40:01

标签: python django django-views

我正在尝试使用ListView和ContextMixin为此创建视图,但我不确定这是否是正确的方法。目标是获取CourseImplementation中的所有字段以及在TeacherCourseImplementation中与其连接的teacherid 问题是我在浏览器上打开模板时看不到部件implement.teacherid。如果我想得到老师的话,我不知道该怎么做。

class ImplementView(generic.ListView):
    template_name = 'schedule/implement.html'
    context_object_name = 'all_implements'

    def get_queryset(self):
        return CourseImplementation.objects.all()

    def get_context_data(self, **kwargs):
        context = super(ImplementView, self).get_context_data(**kwargs)
        context['teacherid'] = TeacherCourseImplementation.objects.all()
        return context

这是我的models.py

class CourseImplementation(models.Model):
    courseid = models.ForeignKey(Course, on_delete=models.CASCADE, db_column='courseid', )
    roomid = models.ForeignKey('Room', models.DO_NOTHING, db_column='roomid', blank=True, null=True)
    note = models.CharField(max_length=10, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'course_implementation'

    def __str__(self):
        return self.pk + ' - ' + self.courseid

class TeacherCourseImplementation(models.Model):
    teacherid = models.ForeignKey(Teacher, on_delete=models.CASCADE, db_column='teacherid', primary_key=True)
    course_impleid = models.ForeignKey(CourseImplementation, on_delete=models.CASCADE, db_column='course_impleid')
    p1 = models.IntegerField(blank=True, null=True)
    p2 = models.IntegerField(blank=True, null=True)
    p3 = models.IntegerField(blank=True, null=True)
    p4 = models.IntegerField(blank=True, null=True)
    p5 = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'teacher_course_implementation'
        unique_together = (('teacherid', 'course_impleid'),)

    def __str__(self):
        return self.pk + ' - ' + self.teacherid

这是我的模板:

<ul>
    {% for implement in all_implements %}
        <div class="col-sm-5 col-lg-5">
            <div class="thumbnail">
                <p>{{ implement.teacherid }} - {{ implement.courseid }}</p>
            </div>
        </div>
    {% endfor %}
</ul>

任何人都可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用TeacherCourseImplementation访问TeacherCourseImplementationimplement的相关implement.teachercourseimplementation_set.all()项目(不要在模板中使用括号):

{% for implement in all_implements %}
    <div class="col-sm-5 col-lg-5">
        <div class="thumbnail">
            <p>{{ implement.courseid }}</p>
            {% for teacher_course_implementation in implement. teachercourseimplementation_set.all %}
              {{ teacher_course_implementation.teacherid }}
              ...
            {% endfor %}
        </div>
    </div>
{% endfor %}

有关详细信息,请参阅following relationships backwards上的文档。

这将为查询集中的每个项生成额外查询。您可以使用prefetch_related来避免这种情况。

def get_queryset(self):
    return CourseImplementation.objects.all().prefetch_related('teachercourseimplementation_set')

由于您通过TeacherCourseImplementation个实例访问了所有CourseImplementation个实例,因此您无需覆盖get_context_data