Reputation: 25
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
Reputation: 129
You can not use the dot (.) operator on dictionary keys. try this:
for i in vwmare:
print(i["hostname"])
Upvotes: 4
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