2016-07-31 27 views
0

這裏是我的模型:無法追加EmbeddedDocument上的保存()

class Subscriber(Document): 
    service = StringField() 
    history = EmbeddedDocumentListField('SubscriberHistory') 

    def __str__(self): 
     return self.service 

class SubscriberHistory(EmbeddedDocument): 
    action = StringField() 
    content = DictField() 
    created_at = DateTimeField(required=True, default=datetime.utcnow) 

    def __str__(self): 
    return self.action 

和這裏的代碼,在那裏我試圖將嵌入保存到我的文檔:只要

subscriber_history = SubscriberHistory() 
subscriber_history.action = 'inbound', 
subscriber_history.content = event 
self.subscriber.history.append(subscriber_history) 
self.subscriber.save() 

我跑self.subscriber.save()我得到以下錯誤:

File "/foo/bar/env/lib/python3.5/site-packages/mongoengine/base/fields.py", line 415, in validate 
self.error('Invalid %s item (%s)' % (field_class, value), 
TypeError: __repr__ returned non-string (type tuple) 

我的代碼是正確的(我在mongoengine文檔讀取),但它贏得了沒有工作。有任何想法嗎?

回答

1

當您第一次保存文檔時,用戶文檔,使歷史等於「=」與列表

subscriber.history = [subscriber_history] 
subscriber.save() 

以後,當你需要添加更多的歷史,你做的更新操作/查詢,從不附加到mongo列表字段。