2013-05-15 31 views
8

Próbuję pobrać identyfikator określonego obiektu w django, ale nadal pojawia się następujący błąd: Wartość wyjątku: QuerySet; Obiekt nie ma identyfikatora atrybutu. moja funkcja w views.pyQuerySet, Object nie ma identyfikatora atrybutu - Django

@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') 

Błąd występuje w jeśli warunek w tej części (attachedInfo = at.id). Sprawdziłem, że kiedy usunąłem go z tego stanu, wszystko działało dobrze.

Oto 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 

Każda pomoc dlaczego dostaję ten błąd ?!

Odpowiedz

19

ta linia kodu

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

zwraca queryset

i próbujesz uzyskać dostęp do dziedziny IT (który nie istnieje).

co prawdopodobnie trzeba to

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

może to spowodować błąd. Aby poradzić sobie z tym z wdziękiem, wykonaj ".filter" amd, a następnie "[0]' – karthikr

+1

co zwróci, jeśli queryset ma 0 elementów? – EsseTi

+0

musisz sprawdzić puste zapytanie, a następnie wykonać [0] To właśnie miałem na myśli. – karthikr

12

Powodem, dla którego pojawia się błąd, jest to, że at to QuerySet, tj .: lista. Możesz zrobić coś takiego jak at[0].id lub użyć get zamiast filter, aby uzyskać obiekt at.

Mam nadzieję, że pomoże!

+0

Użyłem get zamiast filtra i działa świetnie. Dziękuję Ci bardzo. – omarsafwany

+0

natknąłem się na to i wypróbowałem filtr, a '' pod adresem [0] .id '': obiekt "dict" nie ma atrybutu "id". Może to możesz poprawić? – novski

1

W większości przypadków nie chcą obsługiwać nie istniejących obiektów, takich jak to. Zamiast

ad[0].id 

użytku

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

Jest zalecana Django shortcut do tego.

Powiązane problemy