Python MongoEngine嵌入式文档。添加属性键错误

Python MongoEngine嵌入式文档。添加属性键错误,python,mongodb,pyramid,mongoengine,embedded-documents,Python,Mongodb,Pyramid,Mongoengine,Embedded Documents,我对嵌入的文档进行了修改,添加了日期。我真的需要对数据库进行同样的更改吗?还是可以通过mongoengine进行更改 样本: class History(EmbeddedDocument): historyType = StringField() historyId = StringField(unique=True) date = DateTimeField(required=False) # This change is not in DB class Car(D

我对嵌入的文档进行了修改,添加了日期。我真的需要对数据库进行同样的更改吗?还是可以通过mongoengine进行更改

样本:

class History(EmbeddedDocument):
    historyType = StringField()
    historyId = StringField(unique=True)
    date = DateTimeField(required=False)  # This change is not in DB

class Car(Document):
    history = SortedListField(EmbeddedDocumentField(History), ordering='date')

#Example:
h = History()
h.historyType='Cooltype'
h.historyId = '2'
h.date = datetime.datetime.now()

c = Car.objects().first()
c.history.append(h)
c.save()
这将引发以下问题:

Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "build/bdist.macosx-10.9-intel/egg/mongoengine/document.py", line 228, in save doc = self.to_mongo()
File "build/bdist.macosx-10.9-intel/egg/mongoengine/base/document.py", line 255, in to_mongo value = field.to_mongo(value)
File "build/bdist.macosx-10.9-intel/egg/mongoengine/fields.py", line 739, in to_mongo reverse=self._order_reverse)
KeyError: 'date'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“build/bdist.macosx-10.9-intel/egg/mongoengine/document.py”,第228行,在save doc=self.to_mongo()中
文件“build/bdist.macosx-10.9-intel/egg/mongoengine/base/document.py”,第255行,to_mongo value=field.to_mongo(value)
文件“build/bdist.macosx-10.9-intel/egg/mongoengine/fields.py”,第739行,to_mongo reverse=self._order_reverse)
KeyError:“日期”

在开发模式下,我没有问题,因为我可以很容易地删除数据库。但是我应该在生产版本中也能做到这一点,删除数据库不是一种情况。

Ah,找到了解决方案。我必须为history字段设置默认值,因为order子句有一些问题。解决方案:date=DateTimeField(必需=False,默认=datetime.datetime.now())