У меня есть несколько настроенных документов:
class AId(EmbeddedDocument):
name = StringField()
add = StringField()
#since mongoengine does not supporting hashing for embedded docs yet
def __hash__(self):
return super(BaseDocument, self).__hash__()
class A(Document):
id = EmbeddedDocumentField(AId, primary_key=True)
class B(Document):
email = StringField(primary_key=True)
refs = ListField(ReferenceField(A))
Я могу добавить в поле refs в классе B, но когда я пытаюсьпрочитав его, я получаю эту ошибку:
bson.errors.InvalidDocument: cannot encode object: <AId: AId object>, of type: <class 'models.AId'>
Код для чтения и обновления документа B выглядит следующим образом:
# errors out
def get_refs(user):
return B.objects(email=user.pk).first().refs
# works
def add_to_refs(user):
# this document is already inserted in the collection for class A
a_id = AId(name='Sample', add='home')
a = A.objects(id=a_id).first()
B.objects(email=user.pk).update_one(push__refs=a)
Использование следующих зависимостей:
mongoengine==0.18.2
pymongo==3.9.0