保存时提供给EmbeddedDocumentField的无效嵌入式文档实例 [英] Invalid embedded document instance provided to an EmbeddedDocumentField on save

查看:520
本文介绍了保存时提供给EmbeddedDocumentField的无效嵌入式文档实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我声明了以下这些mongoengine模型:

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

我尝试基本的

>>> 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并保存航班,这到底是什么问题?

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??

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

>>> 

我不明白为什么会收到此错误.

I don't understand why I get this error.

推荐答案

我最终切换了模型声明的顺序,并且它确实起作用了(即在引用了声明了模型(??? ))

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()

这篇关于保存时提供给EmbeddedDocumentField的无效嵌入式文档实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆