MongoDB / MongoEngine: ValidationError at / [ObjectId('53f466e60ffa5927709972e8')]不是一个有效的目标。

时间:2022-08-30 14:26:58

How do I get the actual string ID from ObjectId('53f466e60ffa5927709972e8')?

如何从ObjectId中获取实际的字符串ID ('53f466e60ffa5927709972e8')?

This is the line that causes the error

这是导致错误的直线。

humans = [humanInstance[0].id]

Update: I did

更新:我做

humans = [str(humanInstance[0].id)]

and now I get

现在我得到

['53f466e60ffa5927709972e8'] is not a valid ObjectId

Why is this not a valid ObjectID, and how do I get one? :/

为什么这不是一个有效的目标,我怎么得到一个呢?:/

1 个解决方案

#1


0  

The good news is '53f466e60ffa5927709972e8' looks like a valid ObjectId string:

好消息是“53f466e60ffa5927709972e8”看起来是一个有效的目标字符串:

a valid ObjectId (12 byte binary or 24 character hex string)

一个有效的目标(12字节二进制或24字符十六进制字符串)

src: PyMongo ObjectId

src:PyMongo ObjectId

You can convert to string as you are doing or via:

你可以转换为字符串,你正在做或通过:

"%s" % humanInstance[0]

Or to get a list use a comprehension:

或者得到一个列表,使用一个理解:

["%s" human for human in humanInstance]

However, your error is because it expects humans to be an ObjectId or a convertible to an ObjectId (the string is fine) but you are providing a list!

然而,你的错误在于它期望人类成为客观的或可转换的对象(字符串是好的),但是你提供了一个列表!

['53f466e60ffa5927709972e8'] is not a valid ObjectId

Try setting humans = '53f466e60ffa5927709972e8' or changing your Document schema to be a ListField of ObjectIdFields or ReferenceFields

尝试设置人类= '53f466e60ffa5927709972e8'或将文档模式更改为ObjectIdFields或ReferenceFields的ListField。

#1


0  

The good news is '53f466e60ffa5927709972e8' looks like a valid ObjectId string:

好消息是“53f466e60ffa5927709972e8”看起来是一个有效的目标字符串:

a valid ObjectId (12 byte binary or 24 character hex string)

一个有效的目标(12字节二进制或24字符十六进制字符串)

src: PyMongo ObjectId

src:PyMongo ObjectId

You can convert to string as you are doing or via:

你可以转换为字符串,你正在做或通过:

"%s" % humanInstance[0]

Or to get a list use a comprehension:

或者得到一个列表,使用一个理解:

["%s" human for human in humanInstance]

However, your error is because it expects humans to be an ObjectId or a convertible to an ObjectId (the string is fine) but you are providing a list!

然而,你的错误在于它期望人类成为客观的或可转换的对象(字符串是好的),但是你提供了一个列表!

['53f466e60ffa5927709972e8'] is not a valid ObjectId

Try setting humans = '53f466e60ffa5927709972e8' or changing your Document schema to be a ListField of ObjectIdFields or ReferenceFields

尝试设置人类= '53f466e60ffa5927709972e8'或将文档模式更改为ObjectIdFields或ReferenceFields的ListField。