我正在尝试为学生制作一个应用程序,但我收到了此错误,正如您所看到的,为了方便起见,我想使用如下所示的应用程序,但它不起作用,我应该像以前一样继续使用吗?或者我可以这样使用?最好的办法是什么?
/student/program_struct/ 处的 ValueError 无法查询“ abcd@gmail.com ”:必须是“学生”实例。
太感谢了 :)
models.py
class Student(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
status = models.CharField(max_length=10, choices=STATUS, default='active')
class Program(models.Model):
#everything was fine when used
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) #this one
#but when using this one i started getting this error from views.py
user = models.ForeignKey(
Student, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, unique=True)
prefix = models.CharField(max_length=20)
class Course(models.Model):
user = models.ForeignKey(Student, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, unique=True)
prefix = models.CharField(max_length=20)
code = models.CharField(max_length=20)
subject = models.ManyToManyField('Subject', related_name='subject_list',
blank=True)
views.py
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = get_object_or_404(Student, user=self.request.user)
program_structure = Course.objects.filter(student=profile)
# program_structure =
Course.objects.filter(student__user=self.request.user)
credit = Course.objects.filter(student__user=self.request.user).
annotate(total_no=Sum('subject__credit'))
total_credit = self.request.user.course_set.aggregate(
total_credit=Sum('subject__credit')
)['total_credit'] or 0
context = {
'test':program_structure,
'credit':credit,
'profile':profile,
'total_credit' : total_credit
}
return render(self.request, 'program_structure.html', context)
jeck猫
相关分类