1

我在 Django 项目中使用 python-keystoneclient 模块基本上是为了学习。

环境:

python 3.6.3
django 1.10.8
python-keystoneclient 3.14.0

我对返回对象列表的 api 进行了查询

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>

如您所见,列表中的第二项具有比其他项更多的属性

当我使用以下

for p in projects:
    print(p.id)

我得到这样的结果

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

如果我尝试这样做

for p in projects:
    print(p.createddate)

我收到这个错误

~/.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

我期待这能用 True 打印一行。

如果我使用 ipython 并运行

页。使用标签,我看到其他属性不可用

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

所以其他属性不可用。我错过了一步吗?

如果我出错了,我将不胜感激。

4

1 回答 1

1

您也许可以这样做:

for p in projects:
    if hasattr(p, 'createddate'):
        print(p.createddate)
于 2018-01-26T16:13:31.167 回答