[英]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.