1 回答
TA贡献1909条经验 获得超7个赞
该user字段Course指的是Student对象,而不是User对象,因此您不能使用request.user它。
但是,您可以查询Coursewhere the useris a Studentwhere the useris request.user:
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = Student.objects.all()
program_structure = Course.objects.filter(user__user=self.request.user)
context = {
'test':program_structure,
'profile':profile,
}
return render(self.request, 'program_structure.html', context)
您可能还想设置profile为Student用户的对象。在这种情况下,您可以在过滤s时重用:profileCourse
from django.shortcuts import get_object_or_404
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = get_object_or_404(Student, user=request.user)
program_structure = Course.objects.filter(user=profile)
context = {
'test':program_structure,
'profile':profile,
}
return render(self.request, 'program_structure.html', context)
将该user字段重命名为student:
class Course(models.Model):
student = models.ForeignKey(
Student,
on_delete=models.SET_NULL,
null=True
)
# …
因为这清楚地表明这是 a Student,而不是 a User。在这种情况下,您可以使用以下内容进行过滤:
from django.shortcuts import get_object_or_404
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = get_object_or_404(Student, user=request.user)
program_structure = Course.objects.filter(student=profile)
context = {
'test':program_structure,
'profile':profile,
}
return render(self.request, 'program_structure.html', context)
添加回答
举报