Reputation: 3811
I'm trying to fetch the id of a certain object in Django but I keep getting the following error:
Exception Value: QuerySet; Object has no attribute id.
My function in 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 and when I removed it from the condition, everything started working fine.
Here's 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
Can anyone help me understand why I'm getting this error?!
Upvotes: 37
Views: 135299
Reputation: 121
I got this error for almost 2 days, the main issue for this error solely depends on two files i.e.
models.py & views.py
I was getting this error because I wanted to create session from email id but it shows their is no attribute email so it wasn't fetching any str object.
Solution:-
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)
Create a string for the following you want data from according to your project.
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})
Use .first() method with your Model.objects method. This have resolved my problem hope it would resolves yours too.
Upvotes: 2
Reputation: 91
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 shortcut for that.
Upvotes: 5
Reputation: 4271
this line of code
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)
Upvotes: 73
Reputation: 29794
The reason why you are getting the error is because at
is a QuerySet
ie: a list. You can do something like at[0].id
or use get
instead of filter
to get the at
object.
Hope it helps!
Upvotes: 27