def ToMessage(self, fields=None):
    proto_model = self.ProtoModel(fields=fields)

    proto_args = {}
    for field in proto_model.all_fields():
      name = field.name
      value_property = _VerifyProperty(self.__class__, name)

      # Since we are using getattr rather than checking self._values, this will
      # also work for properties which have a default set
      value = getattr(self, value_property._code_name)
      if value is None:
        continue

      if field.repeated:
        if not isinstance(value, (list, tuple)):
          error_msg = ('Property %s is a repeated field and its value should '
                       'be a list or tuple. Received: %s' % (name, value))
          raise TypeError(error_msg)

        to_add = [ToValue(value_property, element) for element in value]
      else:
        to_add = ToValue(value_property, value)
      proto_args[name] = to_add

    return proto_model(**proto_args)
Converts an entity to an ProtoRPC message.
dataset/ETHPy150Open GoogleDeveloperExperts/experts-app-backend/endpoints_proto_datastore/ndb/model.py/EndpointsModel.ToMessage