Reputation: 1783
I want to test the nested message on protobuf's API on python.
My proto ndemo.proto
file was:
package ndemotest;
message BaseRequest
{
required bytes Key = 1;
}
message ContRequest
{
required BaseRequest baseRequest = 1;
optional string Url = 2;
}
My python ndemo.py
code was:
import binascii
import ndemo_pb2
contReq = ndemo_pb2.ContRequest()
contReq.Url="www.google.com"
base_request = contReq.baseRequest.add()
base_request.Key="12345"
packed_data = contReq.SerializeToString()
print 'sending "%s"' % binascii.hexlify(packed_data)
When I ran my script as python ndemo.py
, there was an error
Traceback (most recent call last): File "ndemo.py", line 8, in base_request = contReq.baseRequest.add() AttributeError: 'BaseRequest' object has no attribute 'add'
Upvotes: 2
Views: 7729
Reputation: 1372
You only have add()
for a repeated
field, that's the point of it.
In your case, as baseRequest
is required
, you should simply assign the value directly to the field within BaseRequest
, like:
contReq = ndemo_pb2.ContRequest()
contReq.baseRequest.key = "12345"
Upvotes: 6