Python django ValueError无法查询“;abcd@gmail.com&引用;:必须是;“对象”;实例

Python django ValueError无法查询“;abcd@gmail.com&引用;:必须是;“对象”;实例,python,django,django-models,django-views,django-templates,Python,Django,Django Models,Django Views,Django Templates,我正在尝试为学生制作一个应用程序,但我遇到了这个错误, 正如你们所看到的,为了方便起见,我想像我下面展示的那个一样使用,但它不起作用,我应该像以前一样继续使用吗?或者我可以那样用?最好的办法是什么 在/student/program\u结构处出现值错误/ 无法查询“abcd@gmail.com“:必须是“学生”实例。 非常感谢:) 课程的用户字段指的是学生对象,而不是用户对象,因此您不能使用请求。用户为此 但是,您可以查询课程,其中用户是学生,用户是请求。用户具有: class Program_

我正在尝试为学生制作一个应用程序,但我遇到了这个错误, 正如你们所看到的,为了方便起见,我想像我下面展示的那个一样使用,但它不起作用,我应该像以前一样继续使用吗?或者我可以那样用?最好的办法是什么

在/student/program\u结构处出现值错误/ 无法查询“abcd@gmail.com“:必须是“学生”实例。

非常感谢:)


课程的
用户
字段
指的是
学生
对象,而不是
用户
对象,因此您不能使用
请求。用户
为此

但是,您可以查询
课程
,其中
用户
学生
用户
请求。用户
具有:

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)
最好将
user
字段重命名为
student

class Course(models.Model):
    student = models.ForeignKey(
        Student,
        on_delete=models.SET_NULL,
        null=True
    )
    # …

请出示您的
课程
模型我已经添加了您可以在/student/program\u structure/'CustomUser'对象没有属性'Course\u set'先生我现在收到这个错误您还记得吗,你确实解决了:)你能检查一下我有编辑视图吗?我怎么解决这个问题?@Rafikhan:这是在课程检索下面,而不是
total\u credit=self.request.user.course\u set.aggregate(…)
,你使用
total\u credit=program\u structure.aggregate(…)
。这太神奇了,你可以出版一本书,我们可以向你学习,先生。传统书籍教程并没有讨论这些问题,非常感谢。我想听听你的建议,怎样才能在django变得更好,蟒蛇我有个问题,你能帮我查一下吗
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)
class Course(models.Model):
    student = models.ForeignKey(
        Student,
        on_delete=models.SET_NULL,
        null=True
    )
    # …
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)