我正在尝试获取 Django 中某个对象的 id,但一直收到以下错误:异常值:QuerySet;对象没有属性 id。views.py 中的我的函数:@csrf_exempt def
我正在尝试获取 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
有人能帮助我理解为什么我会收到这个错误吗?!
QuerySet,对象没有属性 id - Django
下载声明:
本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!