title: One to many relationship in Mongoengine
body: <p>I am trying to model one to many relationship with two models named <code>Portfolios</code> and <code>PortfolioModels</code> . The data model looks like the below image.</p>
  
  <p><img src="https://i.stack.imgur.com/rkHn7.png" alt="enter image description here"></p>
  
  <p>I am using MongoEngine to generate the schema. The code which i have written so far is shown below.</p>
  
  <pre><code>import datetime
  from mongoengine import Document, StringField, DateTimeField, ListField, EmbeddedDocument, ReferenceField, \
      EmbeddedDocumentField, EmbeddedDocumentListField
  
  from mongoengine import connect
  
  connect('test')
  
  
  class PortfolioModels(EmbeddedDocument):
      PortfolioId = ReferenceField('Portfolios')
      ModelName = StringField()
      CreateDate = DateTimeField()
      LastUpdateDate = DateTimeField(default=datetime.datetime.now)
  
      def save(self, *args, **kwargs):
          if not self.CreateDate:
              self.CreateDate = datetime.datetime.now()
          self.LastUpdateDate = datetime.datetime.now()
          return super(PortfolioModels, self).save(*args, **kwargs)
  
  
  class Portfolios(Document):
      PortfolioName = StringField(required=True)
      LastUpdateDate = DateTimeField(default=datetime.datetime.now)
      RecentActivity = ListField()
      PortfolioModels = EmbeddedDocumentListField('PortfolioModels')
  
      def __str__(self):
          return self.PortfolioName
  
  
  if __name__ == '__main__':
      a = Portfolios(
          PortfolioName="Best Buy Visa Portfolio",
          RecentActivity=[
              "Data Refreshed by JV on 04-07-2015",
              "Model Updated by SM on 04-12-2015"
          ]
      )
  
      b = PortfolioModels(
          ModelName="Baseline Model"
      )
      a.save()
  </code></pre>
  
  <p>The code gives me output like this.</p>
  
  <pre><code>{
      "Id": ObjectId("34545fggdeesrhygsdf"),
      "PortfolioName": "General Motors MasterCard Portfolio",
      "LastUpdateDate": "2015-04-23T18:25:43.511Z",
      "RecentActivity": [
        "Data Refreshed by JV on 04-07-2015",
        "Model Updated by SM on 04-12-2015"
      ],
      "PortfolioModels": []
    }
  </code></pre>
  
  <p>The expected output is like this as shown below. What i am missing.</p>
  
  <pre><code>[
    {
      "Id": "12345",
      "PortfolioName": "Best Buy Visa Portfolio",
      "LastUpdateDate": "2015-04-23T18:25:43.511Z",
      "RecentActivity": [
        "Data Refreshed by JV on 04-07-2015",
        "Model Updated by SM on 04-12-2015"
      ],
      "PortfolioModels": [
        {
          "Id": "24079",
          "PortfolioId": "12345",
          "ModelName": "Baseline Model",
          "CreateDate": "2015-04-23T18:25:43.511Z",
          "LastUpdateDate": "2015-04-23T18:25:43.511Z"
        },
        {
          "Id": "83623",
          "PortfolioId": "12345",
          "ModelName": "Baseline Model Old",
          "CreateDate": "2013-06-23T18:25:43.511Z",
          "LastUpdateDate": "2013-08-23T18:25:43.511Z"
        }
      ]
    },
    {
      "Id": "08938",
      "PortfolioName": "General Motors MasterCard Portfolio",
      "LastUpdateDate": "2015-04-23T18:25:43.511Z",
      "RecentActivity": [
        "Data Refreshed by JV on 04-07-2015",
        "Model Updated by SM on 04-12-2015"
      ],
      "PortfolioModels": []
    }
  ]
  </code></pre>