我必须根据此选择结果表: 选择课程->选择课程->这将显示所有学生的结果
问题是我无法查询参加该课程的所有特定学生。
我的结果模型:
class Course(models.Model):
cid = models.AutoField(primary_key=True)
class Student(models.Model):
sroll = models.IntegerField()
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Session(models.Model):
sesid = models.IntegerField(primary_key=True,verbose_name= ('Session'))
class Registration(models.Model):
session = models.ForeignKey(Session, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
class Assignation(models.Model):
reg = models.ForeignKey(Registration, on_delete=models.CASCADE, null=True)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, null=True)
class Result(models.Model):
asign = models.ForeignKey(Assignation, on_delete=models.CASCADE, null=True)
student = models.ForeignKey(Student, on_delete=models.CASCADE, null=True)
ct1 = models.FloatField(null=True, blank=True)
我的views.py具有:
@method_decorator(login_required, name='dispatch')
class SelectSession(ListView):
template_name = 'Dashboard/selectsession.html'
model = Session
fields = ['sesid']
def get_queryset(self):
return Session.objects.all()
@method_decorator(login_required, name='dispatch')
class SelectCourse(ListView):
template_name = 'Dashboard/selectcourse.html'
model = Course, Registration
fields = ['all']
def get_queryset(self):
course = Registration.objects.filter(session_id=self.kwargs['pk'])
return course
@method_decorator(login_required, name='dispatch')
class BatchResult(ListView):
template_name = 'Dashboard/batchResult.html'
model = Result,Student,Registration,Assignation
fields = ['all']
def get_queryset(self):
result=Result.objects.filter(???=self.kwargs['pk'])
return result
答案 0 :(得分:0)
使用pk
代替session_pk
来保持代码的可读性。
在session_id
类的session
函数下,它应该是get_queryset()
而不是SelectCource
def get_queryset(self):
course = Registration.objects.filter(session=self.kwargs['pk'])
return course
对于特定的会话,还将有多个课程,这意味着course
响应中将包含多个对象-
def get_queryset(self):
course = Registration.objects.filter(session=self.kwargs['pk'])
assignation_objects = Assignation.objects.filter(reg__in=course)
result = Result.objects.filter(assign__in=assignation_objects )
return result
请参阅Queryset API参考https://docs.djangoproject.com/en/2.1/ref/models/querysets/#in,以获取有关使用查询集的更多详细信息