保存时提供给 EmbeddedDocumentField 的无效嵌入文档实例

Invalid embedded document instance provided to an EmbeddedDocumentField on save

我声明了这些 mongoengine 模型:

class SyncDiscrepancy(EmbeddedDocument):
    upi = StringField(primary_key=True)
    error_code = IntField(required=True)

    meta = {
        'indexes': ['upi', 'error_code']
    }
########## END SYNC


class Flight(Document):
    identifier = StringField(primary_key=True)
    env = StringField(required=True, max_length=3)
    peak = IntField(required=True)
    carrier = StringField(required=True, max_length=3)
    number = IntField(required=True)
    boardpoint = StringField(required=True)
    offpoint = StringField(required=True)
    date = DateTimeField(required=True)
    status = StringField(required=True)
    # store comments
    comments = StringField()

    last_modified = DateTimeField(required=True)

    local_discrepancies = ListField(EmbeddedDocumentField(LocalDiscrepancy))
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))
    count_local = IntField(required=True)
    count_sync = IntField(required=True)

    meta = {
        'indexes': ['_id', 'env','peak', 'date'],
        'ordering': ['-date']
    }

然后我尝试基本的

>>> sy = SyncDiscrepancy(upi='axzdsa', error_code=2)
>>> fl = Flight()
>>> fl.sync_discrepancies.append(sy)
>>> fl.save()
Traceback (most recent call last):

File "<debugger>", line 1, in <module>

fl.save()

File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 224, in save

self.validate(clean=clean)

File "/usr/lib/python2.7/site-packages/mongoengine/base/document.py", line 323, in validate

raise ValidationError(message, errors=errors)

ValidationError: ValidationError (Flight:None) (Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies'] Field is required: ['status', 'count_local', 'offpoint', 'identifier', 'number', 'boardpoint', 'last_modified', 'peak', 'env', 'carrier', 'date', 'count_sync'])

现在我知道我没有填写 flight 的必填字段,但即使我填写了,我仍然会收到此错误 Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies']。我如何声明 SyncDiscrepancy 并保存航班到底有什么问题??

使用mongoengine==0.8.7

编辑 将其缩短为

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))

和:

>>> sy = SyncDiscrepancy(error_code=2)
>>> fl = Flight()
>>> fl.sync_discrepancies.append(fl)
>>> fl.save()
Traceback (most recent call last):

File "<debugger>", line 1, in <module>

fl.save()

File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 224, in save

self.validate(clean=clean)

File "/usr/lib/python2.7/site-packages/mongoengine/base/document.py", line 323, in validate

raise ValidationError(message, errors=errors)

ValidationError: ValidationError (Flight:None) (Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies'])

>>> 

我不明白为什么会出现此错误。

我最终改变了模型声明的顺序并且它起作用了(即在 之后声明模型 被引用 (???))

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField('SyncDiscrepancy'))

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

我被这个错误弄疯了,在我的例子中,嵌入文档 类 按名称引用(使用引号 ' 符号)并删除引号解决了问题。

我有这样的事情:

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField('SyncDiscrepancy'))

改成这样

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))

已解决问题。