第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

django ValueError 無(wú)法查詢“ abcd@gmail.com ”:必須是“對(duì)象”實(shí)例

django ValueError 無(wú)法查詢“ abcd@gmail.com ”:必須是“對(duì)象”實(shí)例

慕田峪4524236 2023-12-05 15:18:38
我正在嘗試為學(xué)生制作一個(gè)應(yīng)用程序,但我收到了此錯(cuò)誤,正如您所看到的,為了方便起見(jiàn),我想使用如下所示的應(yīng)用程序,但它不起作用,我應(yīng)該像以前一樣繼續(xù)使用嗎?或者我可以這樣使用?最好的辦法是什么?/student/program_struct/ 處的 ValueError 無(wú)法查詢“ abcd@gmail.com ”:必須是“學(xué)生”實(shí)例。太感謝了 :)models.pyclass 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)
查看完整描述

1 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

該user字段Course指的是Student對(duì)象,而不是User對(duì)象,因此您不能使用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)

您可能還想設(shè)置profile為Student用戶的對(duì)象。在這種情況下,您可以在過(guò)濾s時(shí)重用: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

    )

    # …

因?yàn)檫@清楚地表明這是 a Student,而不是 a User。在這種情況下,您可以使用以下內(nèi)容進(jìn)行過(guò)濾:


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)


查看完整回答
反對(duì) 回復(fù) 2023-12-05
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)