我宣布了这些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'])
>>>
我不明白为什么会收到此错误。
答案 0 :(得分:3)
我最终切换了模型声明的顺序,它刚刚起作用(即声明模型在被引用之后(???))
class Flight(Document):
sync_discrepancies = ListField(EmbeddedDocumentField('SyncDiscrepancy'))
class SyncDiscrepancy(EmbeddedDocument):
error_code = IntField()
答案 1 :(得分:0)
我为这个错误而疯狂,在我的情况下,嵌入式文档类按名称引用(使用引号'
表示)并删除引号解决了该问题。
我有这样的东西:
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))
解决了该问题。