Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products. If you have feedback or questions as you navigate the site, click Send Feedback.

Python 2 is no longer supported by the community. We recommend that you migrate Python 2 apps to Python 3.

The MessageField Class

Stay organized with collections Save and categorize content based on your preferences.

The MessageField class is used to define messages for efficient transmission across network or process space. Messages are defined using field classes.

MessageField is provided by the protorpc.messages module.

Constructor

The constructor of the MessageField class is defined as follows:

class MessageField(message_type, number, [required=False | repeated=False])

Defines fields for sub-message values.

Arguments
message_type
The message type for the field. Must be a subclass of Message.
number
Number of the field. Must be unique per message class.
required=False
Whether or not this field is required. Mutually exclusive with the repeated argument; do not specify repeated=True if you use required=True.
repeated=False
Whether or not this field is repeated. Mutually exclusive with the required argument; do not specify required=True if you use repeated=True.

Raises a FieldDefinitionError if the message_type is invalid.

Class Property

The MessageField class provides the following properties:

type
The Python type used for values of this field. For example, in the case of DateTimeField, type is datetime.datetime. For user defined MessageFields, type is the specified Message type.
message_type
The underlying message type used for serialization. Specifically this is the type that you can store on an instance of a Message class. For example, for DateTimeField, the type will be message_types.DateTimeMessage. For normal message fields it will be the protorpc message subclass. For example:
class Sub(messages.Message):
  x = messages.IntegerField(1)

class M(messages.Message):
  sub = messages.MessageField(Sub, 1)
  dt = message_types.DateTimeField(2)

print 'M.sub.type         =', M.sub.type
print 'M.sub.message_type =', M.sub.message_type
print 'M.dt.type          =', M.dt.type
print 'M.dt.message_type  =', M.dt.message_type


=== output ===
M.sub.type                 = <class '__main__.Sub>
M.sub.message_type = <class '__main__.Sub'>
M.dt.type                   = <type 'datetime.datetime'>
M.dt.message_type   = <class 'protorpc.message_types.DateTimeMessage'>