gpt4 book ai didi

python - QuerySet,对象没有属性id - Django

转载 作者:太空狗 更新时间:2023-10-29 17:09:18 27 4
gpt4 key购买 nike

我试图在 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')

错误发生在这部分的if条件中(attachedInfo=at.id)。我检查过,当我将其从条件中删除时,一切正常。

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

对我出现此错误的原因有何帮助?!

最佳答案

这行代码

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

返回 queryset

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

你可能需要的是

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

关于python - QuerySet,对象没有属性id - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com