QuerySet,对象没有属性ID-Django [英] QuerySet, Object has no attribute id - Django

查看:310
本文介绍了QuerySet,对象没有属性ID-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django中获取某些对象的ID,但我不断收到以下错误
异常值:QuerySet;对象没有属性ID。
我在views.py中的功能

I'm trying to fetch the id of certain object in django but I keep getting the following error Exception Value: QuerySet; Object has no attribute id. my function in views.py

@csrf_exempt  
def check_question_answered(request):
    userID = request.POST['userID']
    markerID = request.POST['markerID']
    title=request.POST['question']
    m = Marker.objects.get(id=markerID)
    u = App_User.objects.get(id=userID) 
    print userID
    print markerID
    print title
    # userID='1'
    # markerID='1'
    # title='Hello'
    at = AttachedInfo.objects.filter(attachedMarker=m.id, title=title)
    print 'user'
    print u.id
    print 'marker'
    print m.id
    print 'att'
    print at
    #print at.id
    if(Answer.objects.filter(marker=m.id, user=u.id, attachedInfo=at.id)):
        print 'pass'
        return HttpResponse('already answered')
    else:
        print 'not'
        return HttpResponse('not answered yet') 

错误发生在t如果这部分条件(attachedInfo = at.id)。我检查了一下,就像从条件中删除它一样,一切正常。

The error occurs in the if condition in this part (attachedInfo=at.id). I checked that as when I removed it from the condition, everything was working fine.

这里是models.py

Here's models.py

class AttachedInfo(models.Model):
    title = models.CharField(max_length=200)
    helpText = models.CharField(max_length=200, null=True, blank=True)
    type = models.CharField(max_length=200)
    attachedMarker = models.ForeignKey(Marker)
    answer1 = models.CharField(max_length=200, null=True, blank=True)
    answer2 = models.CharField(max_length=200, null=True, blank=True)
    answer3 = models.CharField(max_length=200, null=True, blank=True)
    answer4 = models.CharField(max_length=200, null=True, blank=True)
    correctAnswer = models.CharField(max_length=50, null=True, blank=True)
    optionalMessage = models.CharField(max_length=200, null=True, blank=True)
    def __unicode__(self):
        return self.title

class Answer(models.Model):
    user = models.ForeignKey(App_User)
    app = models.ForeignKey(App, null=True, blank=True)
    marker = models.ForeignKey(Marker)
    attachedInfo = models.ForeignKey(AttachedInfo)
    textAnswer = models.CharField(max_length=200, null=True, blank=True)
    mcqAnswer = models.CharField(max_length=200, null=True, blank=True)
    answered = models.BooleanField(default=False)
    def __unicode__(self):
        return self.attachedInfo.title

对我为什么会出现此错误的任何帮助吗?!

Any help why I'm getting this error?!

推荐答案

这行代码

at = AttachedInfo.objects.filter(attachedMarker = m.id,title = title)

返回查询集

,而您正在尝试访问该字段(不存在)。

and you are trying to access a field of it (that does not exist).

您可能需要的是

at = AttachedInfo.objects.get(attachedMarker=m.id, title=title)

这篇关于QuerySet,对象没有属性ID-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆