Invalid embedded document instance provided to an EmbeddedDocumentField on save

假如想象 提交于 2019-12-06 02:49:03

问题


I have these mongoengine models declared:

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']
    }

And I try a basic

>>> 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'])

Now I know that I didn't fill the required fields for flight, but even if I do, I still get this error Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies']. What exactly is the problem with how I declare the SyncDiscrepancy and save the flight??

Using mongoengine==0.8.7

EDIT Shortened it to

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

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

and:

>>> 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'])

>>> 

I don't understand why I get this error.


回答1:


I ended up switching the order of the model declarations and it just worked (i.e. declaring the model after being referenced (???))

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

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()



回答2:


I was going crazy with this error and in my case embedded document classes where referenced by name (using a quote ' notation) and removing the quotes fixed the issue.

I had something like this:

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

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

Changing it to this

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

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

fixed the issue.



来源:https://stackoverflow.com/questions/27846322/invalid-embedded-document-instance-provided-to-an-embeddeddocumentfield-on-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!