QuerySet,Object没有属性id - Django

时间:2013-05-15 18:34:36

标签: python django django-models django-views django-queryset

我正在尝试在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

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

4 个答案:

答案 0 :(得分:31)

这行代码

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

返回queryset

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

您可能需要的是

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

答案 1 :(得分:16)

您收到错误的原因是atQuerySet即:列表。您可以执行at[0].id之类的操作或使用get代替filter来获取at对象。

希望它有所帮助!

答案 2 :(得分:2)

在大多数情况下,您不希望不处理这样的现有对象。而不是

ad[0].id

使用

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

这是推荐的Django shortcut

答案 3 :(得分:0)

我将近2天收到此错误,此错误的主要问题仅取决于两个文件,即

models.py&views.py

我收到此错误消息是因为我想从电子邮件ID创建会话,但显示它们不是属性电子邮件,因此未获取任何str对象。

解决方案:-

models.py

class Register(models.Model):
userid = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
email = models.EmailField(max_length=200)
password = models.CharField(max_length=100)

def __str__(self):
    return "%s %s" %(self.name, self.email)

为以下项目创建一个字符串,以根据项目从中获取数据。

views.py

    if request.method == "POST":
        emailx1 = request.POST['emailx']
        passwordx1 = request.POST['passwordx']
        if (Register.objects.filter(email=emailx1, password=passwordx1)).exists():
            a = Register.objects.filter(email=emailx1).first()
            request.session['session_name'] = a.email
            request.session['session_id'] = a.userid
            return render(request, "index.html", {"a": a})

将.first()方法与Model.objects方法一起使用。这已经解决了我的问题,希望也能解决您的问题。