Python 2.7 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Python 2.7 アプリケーションをデプロイできなくなります。既存の Python 2.7 アプリケーションは、
非推奨日以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Python に移行することをおすすめします。
Expando クラス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
注: 新しいアプリケーションを作成する際は、NDB クライアント ライブラリを使用することを強くおすすめします。NDB クライアント ライブラリには、Memcache API によるエンティティの自動キャッシュをはじめ、このクライアント ライブラリにはないメリットがあります。古い DB クライアント ライブラリを現在使用している場合は、DB から NDB への移行ガイドをお読みください。
Expando クラスは、プロパティが動的に決定されるデータモデル定義のスーパークラスです。Expando モデルでは、Model と同様の固定プロパティと実行時にエンティティに割り当てられる動的プロパティの組み合わせを使用できます。
Expando
は google.appengine.ext.db
モジュールによって提供されます。
Expando は Model のサブクラスであり、Model クラスのクラスメソッドとインスタンス メソッドを継承します。Expando クラスが定義またはオーバーライドするメソッドはありません。
はじめに
Expando モデルでは、固定プロパティと動的プロパティを使用できます。固定プロパティは Model のプロパティと同様に動作し、クラス属性を使用して Expando モデルクラス内に同様に定義されます。動的プロパティは、インスタンスでプロパティに値を割り当てると作成されます。同じ Expando クラスの 2 つのインスタンスで、異なる動的プロパティのセットを使用することができます。また、同じ名前で型が異なる動的プロパティを使用することもできます。動的プロパティは常にオプションであるため、デフォルト値はありません。つまり、値を割り当てるまでは存在しません。
動的プロパティでは、Property インスタンスを使用した検証、デフォルト値の設定、値に対する自動ロジックの適用を行うことはできません。動的プロパティは、サポートされているデータストアの型の値を格納するだけです。詳細については、型とプロパティ クラスをご覧ください。
また、固定プロパティとは異なり、動的プロパティではクラス属性名とデータストアのプロパティ名を異なる名前にすることはできません。詳細については、使用できないプロパティ名をご覧ください。
ヒント: Property クラスを使用して動的プロパティの値を検証したい場合、Property クラスをインスタンス化し、そのインスタンスの validate() メソッドを動的プロパティの値に対して呼び出すという方法があります。
Expando サブクラスでは、Model クラスと同じように固定プロパティを定義できます。Expando の固定プロパティは Model のプロパティと同様に動作します。Expando インスタンスでは固定プロパティと動的プロパティの両方を使用できます。
import datetime
from google.appengine.ext import db
class Song(db.Expando):
title = db.StringProperty()
crazy = Song(title='Crazy like a diamond',
author='Lucy Sky',
publish_date='yesterday',
rating=5.0)
hoboken = Song(title='The man from Hoboken',
author=['Anthony', 'Lou'],
publish_date=datetime.datetime(1977, 5, 3))
crazy.last_minute_note=db.Text('Get a train to the station.')
Expando インスタンスの動的(固定でない)プロパティは削除できます。動的プロパティを削除するには、アプリケーションでインスタンスの属性を削除します。
del crazy.last_minute_note
コンストラクタ
Expando クラスのコンストラクタは次のように定義されます。
- class Expando(parent=None, key_name=None, **kwds)
-
使用する前にクラスでプロパティを定義する必要がないモデルクラス。Model と同様に、Expando クラスはサブクラス化してデータ エンティティの種類を定義する必要があります。
Expando は Model のサブクラスであり、Model クラスのメソッドを継承またはオーバーライドします。
引数
- parent
- 新しいエンティティの親エンティティの Model インスタンスまたは Key インスタンス。
- key_name
-
新しいエンティティの名前。この名前は主キーの一部となります。None
の場合、システムが生成した ID がキーとして使用されます。
key_name の値を数字で始めることはできません。また、__*__
の形式にすることもできません。アプリケーションでユーザーが送信したデータ(メールアドレスなど)をデータストアのエンティティ キー名として使用する場合、これらの要件を満たすために、最初にデータの前に既知の文字列(「key:」など)を追加するなどして値をサニタイズする必要があります。
key_name
は、str
の値を ASCII テキストに変換した Unicode 文字列として保存されます。
- **kwds
- キーワード引数として使用するインスタンスのプロパティの初期値。それぞれの名前は新しいインスタンスの属性に対応するものであり、Expando クラスで定義された固定プロパティまたは動的プロパティのいずれかに相当します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 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\u003eDevelopers should prioritize the NDB Client Library over the older DB Client Library for new applications due to benefits like automatic entity caching.\u003c/p\u003e\n"],["\u003cp\u003eThe Expando class allows for the creation of entities with both fixed properties, similar to those in the Model class, and dynamic properties that are assigned at runtime.\u003c/p\u003e\n"],["\u003cp\u003eDynamic properties in Expando are optional, lack default values, and are created when they're assigned a value, and these properties do not support property classes or name aliasing.\u003c/p\u003e\n"],["\u003cp\u003eExpando instances can have a mix of fixed and dynamic properties, with the latter able to be deleted by removing the corresponding attribute from the instance.\u003c/p\u003e\n"],["\u003cp\u003eThe Expando constructor allows setting a parent, key name, and initial property values, supporting both fixed and dynamic properties through keyword arguments.\u003c/p\u003e\n"]]],[],null,["# The Expando Class\n\n**Note:**\nDevelopers building new applications are **strongly encouraged** to use the\n[NDB Client Library](/appengine/docs/legacy/standard/python/ndb), which has several benefits\ncompared to this client library, such as automatic entity caching via the Memcache\nAPI. If you are currently using the older DB Client Library, read the\n[DB to NDB Migration Guide](/appengine/docs/legacy/standard/python/ndb/db_to_ndb)\n\nThe Expando class is a superclass for data model definitions whose properties are determined dynamically. An Expando model can have a combination of fixed properties similar to [Model](/appengine/docs/legacy/standard/python/datastore/modelclass) and dynamic properties assigned to an entity at run-time.\n\n`Expando` is provided by the `google.appengine.ext.db` module.\n\nExpando is a subclass of [Model](/appengine/docs/legacy/standard/python/datastore/modelclass), and inherits its class and instance methods from that class. The Expando class does not define or override any methods.\n\nIntroduction\n------------\n\nAn Expando model can have fixed properties and dynamic properties. Fixed properties behave similar to properties of a [Model](/appengine/docs/legacy/standard/python/datastore/modelclass), and are similarly defined in the Expando model class using class attributes. Dynamic properties are created when they are assigned values on the instance. Two instances of the same Expando class can have different sets of dynamic properties, and can even have dynamic properties with the same name but different types. Dynamic properties are always optional, and have no default value: They don't exist until they are assigned a value.\n\nDynamic properties cannot use [Property](/appengine/docs/legacy/standard/python/datastore/propertyclass) instances to perform validation, set defaults or apply automatic logic to values. Dynamic properties simply store values of the supported datastore types. See [Types and Property Classes](/appengine/docs/legacy/standard/python/datastore/typesandpropertyclasses).\n\nAlso unlike fixed properties, dynamic properties cannot use a different name for the class attribute and the datastore property name. See [Disallowed Property Names](/appengine/docs/legacy/standard/python/datastore/modelclass#Disallowed_Property_Names).\n\n**Tip:** If you want to validate a dynamic property value using a Property class, you can instantiate the Property class and call its [validate()](/appengine/docs/legacy/standard/python/datastore/propertyclass#Property_validate) method on the value.\n\nAn Expando subclass can define fixed properties similar to a [Model](/appengine/docs/legacy/standard/python/datastore/modelclass) class. Fixed properties of an Expando behave similarly to properties of a Model. An Expando instance can have both fixed and dynamic properties. \n\n```python\nimport datetime\n\nfrom google.appengine.ext import db\n\nclass Song(db.Expando):\n title = db.StringProperty()\n\ncrazy = Song(title='Crazy like a diamond',\n author='Lucy Sky',\n publish_date='yesterday',\n rating=5.0)\n\nhoboken = Song(title='The man from Hoboken',\n author=['Anthony', 'Lou'],\n publish_date=datetime.datetime(1977, 5, 3))\n\ncrazy.last_minute_note=db.Text('Get a train to the station.')\n```\n\nAn Expando instance's dynamic (non-fixed) properties can be deleted. To delete a dynamic property, an application deletes the instance's attribute: \n\n```\ndel crazy.last_minute_note\n```\n\nConstructor\n-----------\n\nThe constructor of the Expando class is defined as follows:\n\nclass Expando(parent=None, key_name=None, \\*\\*kwds)\n\n: A model class whose properties do not need to be defined in the class before use. Like [Model](/appengine/docs/legacy/standard/python/datastore/modelclass), the Expando class must be subclassed to define the kind of the data entities.\n\n Expando is a subclass of [Model](/appengine/docs/legacy/standard/python/datastore/modelclass), and inherits or overrides its methods.\n\n Arguments\n\n parent\n : The Model instance or Key instance for the entity that is the new entity's parent.\n\n key_name\n\n : The name for the new entity. The name becomes part of the primary key. If `None`, a system-generated ID is used for the key.\n\n The value for key_name must not start with a number, and must not be of the form `__*__`. If your application uses user-submitted data as datastore entity key names (such as an email address), the application should sanitize the value first, such as by prefixing it with a known string like \"key:\", to meet these requirements.\n\n A `key_name` is stored as a Unicode string, with `str` values converted as ASCII text.\n\n \\*\\*kwds\n : Initial values for the instance's properties, as keyword arguments. Each name corresponds with an attribute of the new instance, and may either correspond with fixed properties defined in the Expando class, or be dynamic properties."]]