Tronic Tronic - 1 year ago 72
Python Question

Can't append EmbeddedDocument on save()

Here are my models:

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

and here's the code, where i try to save the embed into my document:

subscriber_history = SubscriberHistory()
subscriber_history.action = 'inbound',
subscriber_history.content = event

as soon as i run
i get the following error:

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

My code is correct (as I read in the mongoengine docs), but it won't work. Any ideas?

Answer Source

When you first save a document, the subscriber document, make history equals "=" with a list

subscriber.history = [subscriber_history]

Later, when you need to add more histories, you make an update operation/query, you never append to a mongo list field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download