1

假设我有一个名为 Post 的 MongoEngine 文档类:

class Post(Document):
    author = ReferenceField(Author)

如何在不获取实际作者对象的情况下获取帖子的作者 ID?

如果我写

print post.author.id

MongoEngine 对数据库进行查询以获取作者对象。我可以写

print post._data["author"]

author如果字段已被取消引用,则返回 Author 对象,否则返回DBRef 对象。

有这样做的标准方法吗?

4

1 回答 1

1

怎么样:

post.to_mongo()['author'].id
于 2012-11-28T14:30:37.100 回答