One to many relationship in Mongoengine

3.1k views Asked by At

I am trying to model one to many relationship with two models named Portfolios and PortfolioModels . The data model looks like the below image.

enter image description here

I am using MongoEngine to generate the schema. The code which i have written so far is shown below.

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()

The code gives me output like this.

{
    "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": []
  }

The expected output is like this as shown below. What i am missing.

[
  {
    "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": []
  }
]
1

There are 1 answers

0
Fizzy Chan On

try

PortfolioModels = ListField(EmbeddedDocumentField(PortfolioModels)); or

PortfolioModels = EmbeddedDocumentListField(PortfolioModels)

Just remove the single quote and make sure the param you put into EmbeddedDocumentListField is an object(class) but not name(string) of the E mbeddedDocument