Python 2.7 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Python 2.7 アプリケーションをデプロイできなくなります。既存の Python 2.7 アプリケーションは、
非推奨日以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Python に移行することをおすすめします。
Field クラス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Field クラスは、メッセージに対して定義するすべてのフィールドの基本クラスです。開発者はこのクラスを拡張できません。
Field
は protorpc.messages
モジュールによって提供されます。
コンストラクタ
Field クラスのコンストラクタは次のように定義されます。
- class
Field
(message_type,
number,
[required=False | repeated=False],
variant=None,
default=None)
-
Field インスタンスを初期化します。Field の各サブクラスでは、次の項目を定義する必要があります:
VARIANTS
- 該当のフィールドで使用可能な一連のバリアント型。
DEFAULT_VARIANT
- コンストラクタで指定されていない場合は、デフォルトのバリアント型。
引数
- number
- フィールドの番号。メッセージ クラスごとに一意でなければなりません。
- required=False
- このフィールドが必須かどうかを指定します。
repeated
引数と同時に指定することはできません。required
を使用する場合は repeated
を指定しないでください。
- repeated=False
- このフィールドが反復フィールドかどうかを指定します。
required
引数と同時に指定することはできません。repeated=True
も使用する場合、required=True
は指定しないでください。
- variant=None
- 主にプロトコル バッファによって使用される追加のエンコード情報を指定します。これらのバリアントは、
descriptor.proto
で Type 値にマッピングされます。デフォルト値を使用することをおすすめしますが、32 ビット、64 ビット、符号なしなどの値を指定できます。 - default=None
- 基となるリクエストに値が見つからない場合の、フィールドのデフォルト値。
注: デフォルト値は、反復フィールドやメッセージ フィールドには使用できません。
次の例外を送出します:
クラスのプロパティ
FieldList クラスにはプロパティが 1 つあります:
- default()
- フィールドのデフォルト値。
インスタンス メソッド
FieldList インスタンスには、次のメソッドがあります。
- validate(value)
- フィールドに割り当てられた値を検証します。
引数
- value
- 検証する値。
値が想定される型でない場合、ValidationError を送出します。
- validate_default_element(value)
-
デフォルト フィールドに割り当てられた値を検証します。要素ごとに固有です。
一部のフィールドでは、循環定義の参照の場合に必要な、デフォルト型の遅延解決が可能です。この場合、デフォルト値をプレースホルダとすることができ、すべてのメッセージ クラスが定義された後に必要に応じて解決されます。
引数
- value
- 検証するデフォルト値。
値が想定される型でない場合、ValidationError を送出します。
- validate_default(value)
-
フィールドのデフォルト値を検証します。
引数
- value
- 検証するデフォルト値。
値が想定される型でない場合、ValidationError を送出します。
- message_definition()
-
この Field 定義を含むメッセージ定義を取得します。Field を含む Message オブジェクトの定義を返します。Field がメッセージ クラスの外部で定義されている場合は、None を返します。
値が想定される型でない場合、ValidationError を送出します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e class serves as the foundation for all fields within messages and cannot be extended by developers, and is provided by the \u003ccode\u003eprotorpc.messages\u003c/code\u003e module.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e constructor takes parameters like \u003ccode\u003enumber\u003c/code\u003e, \u003ccode\u003erequired\u003c/code\u003e, \u003ccode\u003erepeated\u003c/code\u003e, \u003ccode\u003evariant\u003c/code\u003e, and \u003ccode\u003edefault\u003c/code\u003e to define field properties, and raises exceptions for invalid inputs, such as an invalid default, or mutually exclusive arguments.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e class contains \u003ccode\u003eVARIANTS\u003c/code\u003e and \u003ccode\u003eDEFAULT_VARIANT\u003c/code\u003e properties that define the field's acceptable variant types, and its default variant type respectively.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e instance methods include \u003ccode\u003evalidate\u003c/code\u003e, \u003ccode\u003evalidate_default_element\u003c/code\u003e, \u003ccode\u003evalidate_default\u003c/code\u003e to check values against expectations, and \u003ccode\u003emessage_definition\u003c/code\u003e to find the \u003ccode\u003eMessage\u003c/code\u003e that contains the Field.\u003c/p\u003e\n"],["\u003cp\u003eDefault values for Fields are not permitted for repeated fields or message fields, as specified by the argument \u003ccode\u003edefault=None\u003c/code\u003e in the constructor.\u003c/p\u003e\n"]]],[],null,["# The Field Class\n\nThe Field class is the base class for all fields defined on messages. This class cannot be extended by developers.\n\n`Field` is provided by the `protorpc.messages` module.\n\nConstructor\n-----------\n\nThe constructor of the Field class is defined as follows:\n\nclass\nField\n(message_type,\nnumber,\n\\[required=False \\| repeated=False\\], variant=None, default=None)\n\n: Initializes an Field instance. Each sub-class of Field must define the following:\n\n `VARIANTS`\n : Set of variant types accepted by that field.\n\n `DEFAULT_VARIANT`\n : Default variant type if not specified in constructor.\n **Arguments**\n\n number\n : Number of the field. Must be unique per message class.\n\n required=False\n : Whether or not this field is required. Mutually exclusive with the `repeated` argument; do not specify `repeated` if you use `required`.\n\n repeated=False\n : Whether or not this field is repeated. Mutually exclusive with the `required` argument; do not specify `required=True` if you also use `repeated=True`.\n\n variant=None\n : Provides additional encoding information that is mainly used by protocol buffers. These variants map to the Type values in `descriptor.proto`. Best practice is to use default values, but you can specify values as 32-bit, 64-bit, unsigned, etc.\n\n default=None\n\n : Default value for the field if not found in the underlying request. **Note:** Default values are not permitted for repeated fields or message fields.\n\n Raises the following exceptions:\n\n - [InvalidVariantError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidVariantError) when invalid variant for field is provided.\n - [InvalidDefaultError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidDefaultError) when invalid default for field is provided.\n - [FieldDefinitionError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#FieldDefinitionError) when invalid number provided or mutually exclusive fields are used.\n - [InvalidNumberError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidNumberError) when the field number is out of range or reserved.\n\nClass Property\n--------------\n\nThe FieldList class has one property:\n\ndefault()\n: The default value for the field.\n\nInstance Methods\n----------------\n\nFieldList instances have the following methods:\n\nvalidate(value)\n: Validates a value assigned to a field.\n **Arguments**\n\n value\n : The value to evaluate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nvalidate_default_element(value)\n\n: Validates a value assigned to a default field. Specific to a single element.\n\n Some fields may allow for delayed resolution of default types necessary in the case of circular definition references. In this case, the default value might be a placeholder that is resolved when needed after all the message classes are defined.\n **Arguments**\n\n value\n : Default value to validate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nvalidate_default(value)\n\n: Validates that a field's default value.\n\n **Arguments**\n\n value\n : Default value to validate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nmessage_definition()\n\n: Gets a message definition that contains this Field definition. Returns a definition for the [Message](/appengine/docs/legacy/standard/python/tools/protorpc/messages/messageclass) object that contains the Field. Returns None if Field is defined outside of a message class.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type."]]