如何在ListView中正确使用get_context_data在Django中获取相关实例 [英] How to properly use get_context_data with ListView to fetch related instances in Django
问题描述
我正在尝试使用ListView和ContextMixin为此创建视图,但是我不确定这是否是正确的方法。目的是要在CourseImplementation中获取所有字段,并在TeacherCourseImplementation
中获取与其相连的Teacherid。问题是,当在浏览器中打开模板时,我看不到part.teacherid部分。我不知道该如何获取教员ID。
I am trying to use ListView and ContextMixin to create a view for this but I'm not sure if this is a right way to do it. The goal is to get all the fields in CourseImplementation and the teacherid that connect with it in TeacherCourseImplementation The problem is I don't see the part implement.teacherid when the template is opened on the browser. I don't know how should I do it if I want to get the 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
这是我的模型。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>
有人可以帮我吗?谢谢。
Can anyone help me on this. Thank you.
推荐答案
您可以访问相关的 TeacherCourseImplementation
项目 TeacherCourseImplementation
项目实现
和 implement.teachercourseimplementation_set.all()
(不要在模板中使用括号):
You can access the related TeacherCourseImplementation
items for a TeacherCourseImplementation
item implement
with implement.teachercourseimplementation_set.all()
(don't use parentheses in the template):
{% 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.
这将为查询集中的每个项目生成一个额外的查询。您可以通过使用 prefetch_related来避免这种情况
。
This will generate an extra query for every item in the queryset. You can avoid this by using prefetch_related
.
def get_queryset(self):
return CourseImplementation.objects.all().prefetch_related('teachercourseimplementation_set')
由于您正在访问所有 TeacherCourseImplementation
实例通过 CourseImplementation
实例,您无需覆盖 get_context_data
Since you are accessing all the TeacherCourseImplementation
instances via the CourseImplementation
instances, you don't need to override get_context_data
.
这篇关于如何在ListView中正确使用get_context_data在Django中获取相关实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!