Search code examples
mongoenginemarshmallowflask-restplus

marshmallow EmbeddedDocument doesn't work


I made a simple board api with flask-restplus and mongoengie.

Also use marshmallow for serialize data.

Below code is now I worked.

[model]

class Article(Document):
    no = SequenceField()
    subject = StringField(required=True)
    content = StringField(required=True)
    userid = StringField(required=True)
    comments = ListField(EmbeddedDocumentField(Comment))
    created_at = DateTimeField(default=datetime.datetime.now())
    updated_at = DateTimeField(default=datetime.datetime.now())


class Comment(EmbeddedDocument):
    content = StringField(required=True)
    userid = StringField(required=True)
    created_at = DateTimeField(default=datetime.datetime.now())

[serializer]

class CommentSchema(Schema):
    content = fields.String()
    userid = fields.String()
    created_at = fields.DateTime()

class ArticleSchema(Schema):
    comments = CommentSchema(many=True)
    class Meta:
        fields = ('no', 'subject', 'content', 'userid', 'comments', 'created_at', 'updated_at')

I defined schema follow to model.

In ArticleSchema, to show comments, I definded comments = CommentSchema(many=True) and insert it to fields.

And get article function is here.

def get_all_articles():
    articles = Article.objects.all()
    data, errors = ArticleListSchema(many=True).dump(articles)
    return data

But when I access to it, it occur Internal error and throw error message like this.

TypeError: Object of type Comment is not JSON serializable

After searched in google, I found some interest function, Nested. (https://marshmallow.readthedocs.io/en/3.0/nesting.html)

So I modified schema.

class ArticleSchema(Schema):
    no = fields.Integer()
    subject = fields.String()
    content = fields.String()
    userid = fields.String()
    comments = fields.Nested(CommentSchema())
    created_at = fields.DateTime()
    updated_at = fields.DateTime()

(comments = fields.Nested(CommentSchema())

But it doesn't work properly too.

[result]

{
    "subject": "string",
    "content": "string",
    "userid": "string",
    "updated_at": "2018-11-06T17:04:55.197000+00:00",
    "no": 20,
    "created_at": "2018-11-06T17:04:55.197000+00:00",
    "comments": {}
}

I already insert 2 comments and mongodb result is,

> db.article.find()
{ "_id" : ObjectId("5be14bb61b48d9113e3d1413"), "no" : 20, "subject" : "string", "content" : "string", "userid" : "string", "comments" : [ { "content" : "cosdadas", "userid" : "123123", "created_at" : ISODate("2018-11-06T17:34:44.199Z") }, { "content" : "Second comment", "userid" : "john", "created_at" : ISODate("2018-11-06T17:34:44.199Z") } ], "created_at" : ISODate("2018-11-06T17:04:55.197Z"), "updated_at" : ISODate("2018-11-06T17:04:55.197Z") }

But in API, comments doesn't show. Just empty {}.

Is there any solution here?

Thanks.


Solution

  • [SOLVED]

    Change

    comments = fields.Nested(CommentSchema()) to

    comments = fields.Nested(CommentSchema, many=True) and it works perfectly.