[Django]-Convert python object to protocol buffer object

0👍

I realize that this question is old, yet I found it via the search. For future searchers, I’ll give an answer.

The problem can occur when you pass in a dict but your Protobuf message definition expects an object there. For instance take something like this:

message Inner {
  map<uint32, double> values = 1;
}

message Outer {
  repeated Inner inners = 1;
}

You might have something represented as Python primitives like this:

outer = {"inners": [{1: 1.0}]}

Now if you try to convert that using pb.Outer(**outer), when you will encounter this message. Implicitly it will try to create Inner instances from the dict {1: 1.0}, but that key (1) is not a string.

The solution is to write something like this:

pb.Outer(inners=[pb.Inner(values=x) for x in outer["inners"]])

Leave a comment