使用pyodata,如何列出查询实体的所有可用属性?

huangapple go评论83阅读模式
英文:

Using pyodata, how do I list all available properties of a queried Entity?

问题

我想要动态列出使用PyOData包从odata实体中获取所有可用属性的方法。

以下是我想要做的事情:

service = pyodata.Client(SERVICE_URL, session)

positions = service.entity_sets.TRLPositionSet.get_entities().select('Prop1,Prop2').execute()
for p in positions:
  # 如何实现下面的get_properties函数?
  properties = get_properties(p)
  for prop in properties:
    print(prop.name, getattr(p, prop.name))

我该如何实现get_properties(p)函数以使其返回一个列表['Prop1', 'Prop2']

英文:

I want to dynamically list all available properties from an odata Entity using PyOData package.

Here is what I'd like to do:

service = pyodata.Client(SERVICE_URL, session)

positions = service.entity_sets.TRLPositionSet.get_entities().select('Prop1,Prop2').execute()
for p in positions:
 # how to implement get_properties below?
 properties = get_properties(p)
 for prop in properties:
   print(prop.name, getattr(p, prop.name)

How would I implement the get_properties(p) function to make it return a list ['Prop1', 'Prop2']?

答案1

得分: 1

翻译结果如下:

看看这里
https://github.com/SAP/python-pyodata/issues/53#issuecomment-533261246

for es in client.schema.entity_sets:
    print(es.name)

    properties = es.entity_type.properties()

    for prop in es.entity_type.key_properties:
        print(f' K {prop.name}({prop.type.name})')
        properties.remove(prop)

    for prop in properties:
        print(f' + {prop.name}({prop.type.name})')

    for prop in es.entity_type.nav_properties:
        print(f' + {prop.name}({prop.to_role.entity_type_name})')

在我的情况下我必须使用`es._entity_type`

请注意,我已经将代码中的 "proprties" 更正为 "properties",并将 "key_proprties" 更正为 "key_properties",以及 "nav_proprties" 更正为 "nav_properties",以匹配正确的Python语法。

英文:

Take a look here:
https://github.com/SAP/python-pyodata/issues/53#issuecomment-533261246

for es in client.schema.entity_sets:
print(es.name)

proprties = es.entity_type.proprties()

for prop in es.entity_type.key_proprties:
    print(f' K {prop.name}({prop.typ.name})')
    proprties.remove(prop)

for prop in proprties:
    print(f' + {prop.name}({prop.typ.name})')

for prop in es.entity_type.nav_proprties:
    print(f' + {prop.name}({prop.to_role.entity_type_name})')

In my case I had to use es._entity_type

huangapple
  • 本文由 发表于 2023年6月22日 07:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定