You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PyOData获取查询OData实体的所有可用属性?

实现PyOData实体选中属性的获取函数

要实现get_properties(p)函数,返回通过select()指定的属性列表,这里提供两种实用方案:

方案1:直接读取集合的_select配置

当你调用.select('Prop1,Prop2')后,选中的字段会被存在实体集合的_select属性中,单个实体可以通过_entity_set关联到所属集合,直接拆分字符串即可:

def get_properties(p):
    # 拆分select指定的字段字符串为列表
    return p._entity_set._select.split(',') if p._entity_set._select else []

方案2:筛选实体已加载的属性

如果需要适配更复杂的场景(比如未显式调用select()但仅加载了部分属性),可以遍历实体的_properties字典,筛选出标记为已加载的属性:

def get_properties(p):
    # 提取已加载属性的名称列表
    return [prop.name for prop in p._properties.values() if prop.loaded]

示例验证

将函数代入你的代码中,即可实现预期效果:

service = pyodata.Client(SERVICE_URL, session)

positions = service.entity_sets.TRLPositionSet.get_entities().select('Prop1,Prop2').execute()
for p in positions:
    properties = get_properties(p)
    for prop_name in properties:
        print(prop_name, getattr(p, prop_name))

注意:PyOData的内部属性(前缀为_)可能随版本迭代变化,建议在使用前结合当前版本测试。

内容的提问来源于stack exchange,提问作者neves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 05:52:07