棉花糖嵌入式文档不起作用
marshmallow EmbeddedDocument doesn't work
我用 flask-restplus
和 mongoengie
做了一个简单的板子 api。
也使用 marshmallow
序列化数据。
下面的代码是我现在工作的。
[型号]
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())
[序列化器]
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')
我根据模型定义了架构。
在 ArticleSchema
中,为了显示评论,我定义了 comments = CommentSchema(many=True)
并将其插入到 fields
。
获取文章功能在这里
def get_all_articles():
articles = Article.objects.all()
data, errors = ArticleListSchema(many=True).dump(articles)
return data
但是当我访问它时,它发生内部错误并抛出这样的错误信息。
TypeError: Object of type Comment is not JSON serializable
在google中搜索后,我找到了一些兴趣函数,Nested
。 (https://marshmallow.readthedocs.io/en/3.0/nesting.html)
所以我修改了架构。
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()
)
但它也不能正常工作。
[结果]
{
"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": {}
}
我已经插入了 2 条评论,mongodb 结果是,
> 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") }
但在 API 中,评论不显示。只是空 {}。
这里有什么解决办法吗?
谢谢。
[已解决]
改变
comments = fields.Nested(CommentSchema())
至
comments = fields.Nested(CommentSchema, many=True)
完美运行。
我用 flask-restplus
和 mongoengie
做了一个简单的板子 api。
也使用 marshmallow
序列化数据。
下面的代码是我现在工作的。
[型号]
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())
[序列化器]
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')
我根据模型定义了架构。
在 ArticleSchema
中,为了显示评论,我定义了 comments = CommentSchema(many=True)
并将其插入到 fields
。
获取文章功能在这里
def get_all_articles():
articles = Article.objects.all()
data, errors = ArticleListSchema(many=True).dump(articles)
return data
但是当我访问它时,它发生内部错误并抛出这样的错误信息。
TypeError: Object of type Comment is not JSON serializable
在google中搜索后,我找到了一些兴趣函数,Nested
。 (https://marshmallow.readthedocs.io/en/3.0/nesting.html)
所以我修改了架构。
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()
)
但它也不能正常工作。
[结果]
{
"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": {}
}
我已经插入了 2 条评论,mongodb 结果是,
> 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") }
但在 API 中,评论不显示。只是空 {}。
这里有什么解决办法吗?
谢谢。
[已解决]
改变
comments = fields.Nested(CommentSchema())
至
comments = fields.Nested(CommentSchema, many=True)
完美运行。