问题描述
是否可以使用类似于 EmbeddedDocuments
的东西在 MongoEngine 中存储对 ReferenceField
的引用?我只能将 _id
存储在 StringField
中。以下代码不起作用:
class Child(EmbeddedDocument):
_id = ObjectIdField(required=True,default=lambda: ObjectId())
class Parent(Document):
children = ListField(EmbeddedDocumentField('Child'),required=False)
active_child = ReferenceField('Child',required=False)
child = Child()
parent = Parent()
parent.children.append(child)
parent.active_child = child # this line does trigger the error
parent.save()
它导致以下错误:
Traceback (most recent call last):
File "/home/me/test.py",line 13,in __main__
parent.save()
File "/home/me/lib/python3.7/site-packages/mongoengine/document.py",line 400,in save
doc = self.to_mongo()
File "/home/me/lib/python3.7/site-packages/mongoengine/document.py",line 259,in to_mongo
data = super().to_mongo(*args,**kwargs)
File "/home/me/lib/python3.7/site-packages/mongoengine/base/document.py",line 374,in to_mongo
value = field.to_mongo(value,**ex_vars)
File "/home/me/lib/python3.7/site-packages/mongoengine/fields.py",line 1262,in to_mongo
id_field_name = cls._Meta["id_field"]
KeyError: 'id_field'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)