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

时间:2014-03-14 11:26:18

标签: 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(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'

在开发模式中,我没有任何问题,因为我可以轻松放弃数据库。但是我应该让这个也适用于生产版本并且丢弃数据库不是一个例子。

0 个答案:

没有答案