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

Python requests请求OData接口时如何过滤列表内的嵌套SampleId字段

问题原因与修复方案

1. 直接报错原因

你定义的params_1中$filter对应的元组包含3个元素,而requests库要求params参数接收的元组序列每个元素必须是(参数名, 参数值)的二元组,因此触发了ValueError: too many values to unpack解包错误。

2. 扩展字段过滤规则

由于SampleId是通过Test->Sample扩展得到的关联字段,OData接口过滤关联字段需要写全关联路径,不能直接用SampleId。

修正后的代码

import requests

sampleid_list = [592785, 592787, 592789, 592790, 592791, 592795, 592796]
# 构造符合OData in语法的过滤表达式
sampleid_str = ','.join(map(str, sampleid_list))
filter_expression = f"Test/Sample/SampleId in ({sampleid_str})"

headers = {'Authorization': 'Bearer ' + token}
proxies = {'https': 'proxy.services.dekra.com:8080'}
url1 = 'https://**-**-***.***.com/odata/Results'

params_1 = (
    ('$filter', filter_expression),
    ('$select', 'ResultId, CompletedOn, EnteredValue, EnteredOn, TextValue'),
    ('$count', 'true'),
    ('$expand', 'Test($expand=Sample($select=SampleId)), Test($expand=TestType($select=Name)), Test($select=SpecificationStatus), ResultType($select=Name), Unit($select=Name)'),
)

response_1 = requests.get(url1, headers=headers, proxies=proxies, params=params_1)
r1 = response_1.json()
print(r1)

补充说明

  • 修正时移除了$select和$expand参数末尾的换行符,避免特殊字符导致的接口识别异常
  • 如果你的OData服务版本较低不支持in语法,可以替换为多个or拼接的条件,效果完全一致

内容的提问来源于stack exchange,提问作者Al-Andalus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 21:45:03