Home > database >  Django iterate on values()
Django iterate on values()

Time:12-07

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'

CodePudding user response:

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

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

CodePudding user response:

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

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