How to add additional fields to embedded documents without changing the original model
<p>I want to add extra properties to a document before embedding that into other document, but I don't know how to do that.</p>

<p>Here's my code and what I have tried so far:</p>

<pre class="lang-py prettyprint-override"><code>from mongoengine import *
from datetime import datetime


class User(Document):
    name = StringField(max_length=80, required=True)
    created_at = DateTimeField(default=datetime.now(), required=True)
    updated_at = DateTimeField(default=datetime.now(), required=True)

    meta = {
        'collection': 'users'
    }

    def save(self, *args, **kwargs):
        self.updated_at = datetime.now()
        return super(User, self).save(*args, **kwargs)



class Stream(Document):
    users = EmbeddedDocumentListField(document_type='User')
    created_at = DateTimeField(default=datetime.now(), required=True)
    updated_at = DateTimeField(default=datetime.now(), required=True)

    meta = {
        'collection': 'streams'
    }

    def save(self, *args, **kwargs):
        self.updated_at = datetime.now()
        return super(Stream, self).save(*args, **kwargs)
</code></pre>

<p>When I embed the <code>user document</code> to the stream's users <code>EmbeddedDocumentListField</code>, It will be added and look like this</p>

<pre class="lang-py prettyprint-override"><code>{
    "_id" : ObjectId("57e6123fe8c39b18b1a13431"),
    "users" : [
        {
            "_id" : ObjectId("57e6123fe8c39b18b1a13432"),
            "name": "Rohit Khatri",
            "created_at" : ISODate("2016-09-24T11:12:23.182Z"),
            "updated_at" : ISODate("2016-09-24T11:12:23.301Z")
        }
    ],
    "created_at" : ISODate("2016-09-24T11:12:23.189Z"),
    "updated_at" : ISODate("2016-09-24T11:12:23.323Z")
}
</code></pre>

<p>Now I want to embed the User document with additional properties, like roles. Here's what I have tried:-</p>

<pre class="lang-py prettyprint-override"><code>user = User.objects.create(name='Rohit Khatri')
user.roles = ['admin','writer']

stream = Stream.objects.create()
stream.users.append(user)
stream.save()
</code></pre>

<p>But It doesn't add the roles field, It would be thankful if somebody can help me with this.</p>

<p>Thanks</p>

<p>use update method to set the new attribute</p>

<pre><code>user.update(set__roles = ['admin','writer'])
</code></pre>

python django mongoengine embedded-documents