1

I've made a modification to my embedded document, added a date. Do I really have to make same changes also to database or can it be done via mongoengine?

Sample:

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()

This will raise the following problem:

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'

In develpopment mode I've no issues, since I can easilly drop the db. But I should get this working also with the production version and dropping the db is not a case.

Heikki Mustonen
  • 301
  • 3
  • 15
  • Ah, found the solution. I had to put default-value for the history-field, since the order-clause had some issues with it. Solution: date = DateTimeField(required=False, default=datetime.datetime.now()) – Heikki Mustonen Mar 15 '14 at 12:15

0 Answers0