Wednesday 24 June 2020

In Django Shell I can see the Object but I can't Access it.

A simple one this but I couldn't find the answer with a Google search so I thought I'd list it here. 


What I have is a Profile object that links to my User object.  And I'm trying to get the 'image' data from it. 

Using Shell this is what I was doing 

python manage.py shell

from users.models import Profile
profile = Profile.objects.filter(user=1)
profile.image

Which would return me the message.

 'QuerySet' object has no attribute 'image'

However I know its there because when I run


profile.values() 

I can see it.  

The answer is straight forward and you need to do the following to call the specific item that you need. 

profile = Profile.objects.filter(user=1).first()
You'll then be able to user 'profile.image' to return the data.


No comments: