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 類別的兩個執行個體可有不同的動態屬性組合,甚至可具有名稱相同但類型不同的動態屬性。動態屬性一律是選用屬性,且沒有預設值:這些屬性在獲指派值之前並不存在。
動態屬性不可使用 Property 執行個體執行驗證作業、設定預設值,或為值套用自動邏輯,而只會儲存受支援資料儲存庫類型的值。請參閱「類型和 Property 類別」。
此外,不同於固定屬性,動態屬性無法針對類別屬性和資料儲存庫屬性使用不同名稱。詳情請參閱不允許的屬性名稱一文。
提示:如果您想使用 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
會以 Unicode 字串形式儲存,其中 str
值會轉換成 ASCII 文字。
- **kwds
- 例項屬性的初始值,形式為關鍵字引數。每個名稱會對應到新執行個體的一項屬性,且可能與 Expando 類別中定義的固定屬性對應,或本身即為動態屬性。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[[["\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."]]