[英]How to properly use get_context_data with ListView to fetch related instances in Django
I am trying to use ListView and ContextMixin to create a view but I'm not sure if this is a right way to do it.我正在尝试使用 ListView 和 ContextMixin 创建视图,但我不确定这是否是正确的方法。 The goal is to get all the fields in
CourseImplementation
and the teacherid
that connects to it in TeacherCourseImplementation
.目标是获取
teacherid
CourseImplementation
连接到它的TeacherCourseImplementation
。
The problem is I don't see the part implement.teacherid
when the template is opened on the browser.问题是在浏览器中打开模板时,我没有看到
implement.teacherid
部分。 I don't know how should I do it if I want to get the teacher ID.我不知道如果我想获得教师证我应该怎么做。
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
This is my models.py这是我的模型.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
This is my template:这是我的模板:
<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>
Can anyone help me on this.谁可以帮我这个事。 Thank you.
谢谢你。
You can access the related TeacherCourseImplementation
items for a TeacherCourseImplementation
item implement
with implement.teachercourseimplementation_set.all()
(don't use parentheses in the template):您可以使用
implement.teachercourseimplementation_set.all()
访问TeacherCourseImplementation
项目implement
的相关TeacherCourseImplementation
项目(不要在模板中使用括号):
{% 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 %}
See the docs on following relationships backwards for more info.有关更多信息,请参阅有关向后跟踪关系的文档。
This will generate an extra query for every item in the queryset.这将为查询集中的每个项目生成一个额外的查询。 You can avoid this by using
prefetch_related
.您可以使用
prefetch_related
来避免这种情况。
def get_queryset(self):
return CourseImplementation.objects.all().prefetch_related('teachercourseimplementation_set')
Since you are accessing all the TeacherCourseImplementation
instances via the CourseImplementation
instances, you don't need to override get_context_data
.由于您正在通过
CourseImplementation
实例访问所有TeacherCourseImplementation
实例,因此您不需要覆盖get_context_data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.