How to convert entity to message with @classmethod

192 Views Asked by At

I have this model:

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)

Where UserMessage is a protoRPC object. And want something like this:

user = User.query(User.email == '[email protected]').get()
user_message = user.to_message()
1

There are 1 best solutions below

0
On BEST ANSWER

You can't use a class method here.

There is no self in a classmethod , by convention it is cls and you are passed the class not an instance. to_message should be a normal method.