无法在 save() 上附加 EmbeddedDocument
Can't append EmbeddedDocument on save()
这是我的模型:
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 文档中看到的那样),但它不起作用。有什么想法吗?
当你第一次保存文档时,订阅者文档,使历史等于“=”与列表
subscriber.history = [subscriber_history]
subscriber.save()
稍后,当您需要添加更多历史记录时,您可以进行更新 operation/query,您永远不会附加到 mongo 列表字段。
这是我的模型:
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 文档中看到的那样),但它不起作用。有什么想法吗?
当你第一次保存文档时,订阅者文档,使历史等于“=”与列表
subscriber.history = [subscriber_history]
subscriber.save()
稍后,当您需要添加更多历史记录时,您可以进行更新 operation/query,您永远不会附加到 mongo 列表字段。