Tom Paulick
Tom Paulick

Reputation: 63

Python - extract value from key-value pair

lots of googling done but I'm stumped. I know this is simple but I can't seem to find the format to retrieve the value from a key/value pair when I supply the key in python. I'm using django --

images = Article.objects.filter(pk=self.ID).values("image1", "image2", "image3")

fills images with the following object:

<QuerySet [{'image3': u'', 'image2': u'', 'image1': u'articleImages/django-allauth.png'}]>

So my question is --- I want get get the value for "image1" how do I get that!! I really really really appreciate your help -

I want something like

image1 = images['image1'] ## clearly this doesn't work

Upvotes: 2

Views: 1786

Answers (3)

Ghost of Goes
Ghost of Goes

Reputation: 106

QuerySet seems to be a list of dict, so perhaps the following will work:

image1 = list(images)[0]['image1']

Upvotes: 0

Marat
Marat

Reputation: 15738

Use values_list:

# will return ['articleImages/django-allauth.png']
images = Article.objects.filter(pk=self.ID).values_list("image1", flat=True)

Upvotes: 1

Shivkumar kondi
Shivkumar kondi

Reputation: 6782

try this..

d = [{'image3': u'', 'image2': u'', 'image1': u'articleImages/django-allauth.png'}]


d[0]['image1']

Output:

u'articleImages/django-allauth.png'

Upvotes: 0

Related Questions