Lolm_47
Lolm_47

Reputation: 25

Django iterate on values()

I have somethink like this:

vmware = Vmware.objects.values('pk', 'hostname')

the result :

<QuerySet [{'pk': 1, 'hostname': 'ste1vvcsa'}, {'pk': 3, 'hostname': 'ste1vvcsatest'}]>

I want to iterate on it and retreive the values of pk and hostname

I have an error when I do somethink like this:

for i in vwmare:
  print(i.hostname)

Error : AttributeError: 'dict' object has no attribute 'hostname'

Upvotes: 0

Views: 92

Answers (2)

Mohsen Karimi
Mohsen Karimi

Reputation: 129

You can not use the dot (.) operator on dictionary keys. try this:

for i in vwmare:
    print(i["hostname"])

Upvotes: 4

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476709

It's a dictionary, hence you access the value of a key by subscripting:

for i in vwmare:
    print(i['hostname'])

Upvotes: 1

Related Questions