python - django ValueError 无法查询“abcd@gmail.com”:必须是“对象”实例
问题描述
我正在尝试为学生制作一个应用程序,但出现此错误,您可以看到为方便起见,我想像下面展示的那样使用它,但它不起作用,我应该像以前一样继续使用吗?或者我可以这样使用?什么是最好的方法?
/student/program_structure/ 处的 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)
解决方案
的user
字段是Course
指Student
对象,而不是User
对象,因此您不能request.user
为此用户。
但是,您可以查询 a Course
where the user
is a Student
where the user
is request.user
with:
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时重用:profile
Course
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)
推荐阅读
- lua - Lua 表被覆盖
- java - 使用真实设备在颤振中调试第一个应用程序时出错
- c++ - TBB 并行管道:过滤器时序不一致
- python - 通过 XML 文件和 netsh wlan(或 Python)更改 wifi 配置文件的密码
- python - 平台问题中无法加载级别列表
- python - Matplotlib 和 mpi4py:Tclerror:没有显示名称,也没有 $DISPLAY
- python - Python scipy.stats - 在 invweibull 曲线上获取特定概率的数字
- php - 联系表 7 稍后调用的变量
- python - 为什么这段代码不生成日志文件?
- sql - 将'n'行值转换为'n'列在sql中的一行