万能药EntityCollection.remove删除()不存在

2024-10-03 17:26:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从EntityCollection中的集合中删除某个Entity,但该操作不起作用,导致异常:list.remove(x): x not in list。你知道吗

代码摘录如下:

user.platformSubscriptions.remove(platform)

平台变量实际上在platformSubscriptions集合中。你知道吗


Tags: 代码innot平台removelistentityplatform
1条回答
网友
1楼 · 发布于 2024-10-03 17:26:25

它不起作用的原因可能是因为平台对象没有合适的比较器函数。因此,解决方案是通过比较platformSubscriptions字段来循环platform以找到匹配的id

for subedPlat in user.platformSubscriptions:
    if subedPlat.id == platform.user.platformSubscriptions.remove(subedPlat)

相关问题 更多 >