pythondjangoopenstacksdkopenstack-keystone

How can I get access to an objects attribute when it is part of a list of objects


I am using the python-keystoneclient module within a Django project basically to learn.

The environment:

python 3.6.3
django 1.10.8
python-keystoneclient 3.14.0

I have a query to the api that returns a list of objects

projects = keystone.projects.list()

for p in projects:
    print(p)


<Project description=, domain_id=default, enabled=True, id=1c1a662d73c04f92acce6c50fb75dc3e, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/1c1a662d73c04f92acce6c50fb75dc3e'}, name=alt_demo, parent_id=default>
<Project description=, domain_id=default, enabled=True, id=311f76914cba43e18bfb416c72d8ad5e, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/311f76914cba43e18bfb416c72d8ad5e'}, name=demo, parent_id=default>
<Project createddate=2018-01-26, department=department1, description=Bob3, domain_id=default, emailaddress=email@example.com, enabled=True, familyname=Jones, firstname=Bob, id=4d4dabcc39ac4323a8665ba01efa65d7, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/4d4dabcc39ac4323a8665ba01efa65d7'}, name=PROJ-BOB3, parent_id=default, sandbox=True, telephone=+99999999999, tos=on, username=bjones> 
<Project description=, domain_id=default, enabled=True, id=916daf2b642944729d284643b355e5a2, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/916daf2b642944729d284643b355e5a2'}, name=service, parent_id=default> 
<Project description=, domain_id=efc1097d56b047099645cbdb53a89c13, enabled=True, id=ab80834046864705b47bfd9478cfe715, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/ab80834046864705b47bfd9478cfe715'}, name=swiftprojecttest4, parent_id=efc1097d56b047099645cbdb53a89c13> 
<Project description=Bootstrap project for initializing the cloud., domain_id=default, enabled=True, id=bad74eb9e68546208232310dab8144d0, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/bad74eb9e68546208232310dab8144d0'}, name=admin, parent_id=default> 
<Project description=, domain_id=default, enabled=True, id=d52a43a6f1cb408592b02aaa3dbb618d, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/d52a43a6f1cb408592b02aaa3dbb618d'}, name=invisible_to_admin, parent_id=default> 
<Project description=, domain_id=default, enabled=True, id=f9c8ca81dbd34c07bce3b4bf19af102c, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/f9c8ca81dbd34c07bce3b4bf19af102c'}, name=swiftprojecttest2, parent_id=default> 
<Project description=, domain_id=default, enabled=True, id=fbdb58e9120c49348a0eb5dfb7e984d0, is_domain=False, links={'self': 'http://172.16.100.10/identity/v3/projects/fbdb58e9120c49348a0eb5dfb7e984d0'}, name=swiftprojecttest1, parent_id=default>

As you can see the second item in the list has more attributes than the rest

When I use the following

for p in projects:
    print(p.id)

I get a result like this

1c1a662d73c04f92acce6c50fb75dc3e
311f76914cba43e18bfb416c72d8ad5e
4d4dabcc39ac4323a8665ba01efa65d7
916daf2b642944729d284643b355e5a2
ab80834046864705b47bfd9478cfe715
bad74eb9e68546208232310dab8144d0
d52a43a6f1cb408592b02aaa3dbb618d
f9c8ca81dbd34c07bce3b4bf19af102c
fbdb58e9120c49348a0eb5dfb7e984d0

If I try to do this

for p in projects:
    print(p.createddate)

I get this error

~/.virtualenvs/sandbox/lib/python3.6/site-packages/keystoneclient/base.py in __getattr__(self, k)
    505                 return self.__getattr__(k)
    506
--> 507             raise AttributeError(k)
    508         else:
    509             return self.__dict__[k]

AttributeError: createddate

I was expecting this to print one line with True.

if I use ipython and run

p. with tabs I see that the other atrributes are not available

In [100]: p.
             delete enabled human_id is_loaded name set_loaded
             description get   id  links  NAME_ATTR   to_dict
             domain_id HUMAN_ID is_domain manager parent_id update

So the other attributes are not available. Have I missed out a step?

I would appreciate some help where I went wrong.


Solution

  • You might be able to do this:

    for p in projects:
        if hasattr(p, 'createddate'):
            print(p.createddate)