我已对我的嵌入式文档进行了修改,添加了日期。我是否真的必须对数据库进行相同的更改,还是可以通过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'
在开发模式中,我没有任何问题,因为我可以轻松放弃数据库。但是我应该让这个也适用于生产版本并且丢弃数据库不是一个例子。