python - 如何使用@classmethod将实体转换为消息

标签 python google-app-engine protorpc

我有这个模型:

class User(ndb.Model):
    firstname = ndb.StringProperty(required = True)
    lastname = ndb.StringProperty(required = True)
    email = ndb.StringProperty(required = True)
    birthday = ndb.DateProperty(required = True)

    @classmethod
    def to_message(self):
        return UserMessage(firstname = self.firstname,
                           lastname = self.lastname,
                           email = self.email,
                           birthday_day = self.birthday.day)

其中 UserMessage 是一个 protoRPC 对象。并想要这样的东西:

user = User.query(User.email == 'john@example.com').get()
user_message = user.to_message()

最佳答案

这里不能使用类方法。

classmethod 中没有 self ,按照惯例,它是 cls 并且您传递的是类而不是实例。 to_message 应该是一个普通的方法。

关于python - 如何使用@classmethod将实体转换为消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769836/

相关文章:

google-app-engine - Mux 和 http.HandleFunc 都适用于 Google App Engine 上的 helloworld 端点

python - Pandas DataFrame 上的特定组计算

python - *** ConnectionError : ('Connection aborted.' , error(13, 'Permission denied' )) 试图将文件上传到谷歌云存储时

android - 对于 Android,如何设置 Cloud Endpoint 请求的最长期限?

python - Google App Engine Python - Protorpc && 任务队列

google-app-engine - 支持 Dart for Google Endpoints 中的查询参数吗?

Python MQTT 连接到 Azure Iot Hub

python - 如何垂直连接三个或更多图像?

python - 如何从 gae ndb 查询中获取单个值?