gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:13:38 25 4
gpt4 key购买 nike

我声明了这些 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()

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com