Python QuerySet, Object 没有属性 id - Django

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16572569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:02:06  来源:igfitidea点击:

QuerySet, Object has no attribute id - Django

pythondjangodjango-modelsdjango-viewsdjango-queryset

提问by omarsafwany

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

我试图在 django 中获取某个对象的 id,但我不断收到以下错误异常值:QuerySet; 对象没有属性 id。我在 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') 

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.

错误出现在这部分的if条件中(attachedInfo=at.id)。我检查了一下,当我从条件中删除它时,一切正常。

Here's models.py

这是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?!

任何帮助为什么我收到此错误?!

采纳答案by EsseTi

this line of code

这行代码

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

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

returns a queryset

返回的查询集

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

并且您正在尝试访问它的一个字段(不存在)。

what you probably need is

你可能需要的是

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

回答by Paulo Bu

The reason why you are getting the error is because atis a QuerySetie: a list. You can do something like at[0].idor use getinstead of filterto get the atobject.

您收到错误的原因是因为at是一个QuerySetie: 列表。您可以执行类似at[0].id或使用get而不是filter获取at对象的操作。

Hope it helps!

希望能帮助到你!

回答by oberam-eng

In most cases you do not want to handle not existing objects like that. Instead of

在大多数情况下,您不想像那样处理不存在的对象。代替

ad[0].id

use

get_object_or_404(AttachedInfo, attachedMarker=m.id, title=title)

It is the recommended Django shortcutfor that.

这是推荐的Django 快捷方式