简体   繁体   中英

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

try this..

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


d[0]['image1']

Output:

u'articleImages/django-allauth.png'

Use values_list :

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

QuerySet似乎是字典的列表,因此也许可以使用以下命令:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM