埋め込みドキュメントを変更し、日付を追加しました。データベースにも同じ変更を加える必要がありますか、それとも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'
開発モードでは、データベースを簡単に削除できるため、問題はありません。しかし、これは製品版でも機能するはずであり、データベースを削除することはできません。