繁体   English   中英

mongoengine QuerySet.to_json不调用Document.to_json

[英]mongoengine QuerySet.to_json doesn't call Document.to_json

我在mongoengine Document的子类Foo上重写了to_json方法。 当我调用Foo.objects()。to_json()时,未使用我的替代。 有没有一种方法可以确保将文档序列化以供演示,而无需重复我自己?

class Foo(Document): presentation_field = StringField(required=True, unique=True) technical_field = StringField(required=True, unique=True)

 def to_json(self): data = self.to_mongo() data.pop('technical_field') return bson.json_util.dumps(data) 

foos = Foo.objects() for foo in foos: print foo.to_json() #override is used!!!! print foos.to_json() # override is ignored :(

这似乎是功能而不是错误。 正确的方法是Foo.objects()。only('presentation_field')进行列过滤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM