我宣布了这些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
EDIT 缩短为
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()

关于python - 保存时提供给EmbeddedDocumentField的无效嵌入式文档实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27846322/

10-10 11:35