Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
Expando 类
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:强烈建议构建新应用的开发者使用 NDB 客户端库,它与 DB 客户端库相比具有多项优势,例如可通过 Memcache API 进行自动实体缓存。如果您当前使用的是较早的 DB 客户端库,请参阅 DB 到 NDB 的迁移指南
Expando 类是用于数据模型定义(其属性动态确定)的超类。Expando 模型可以具有类似于 Model 的固定属性和在运行时分配给实体的动态属性的组合。
Expando
由 google.appengine.ext.db
模块提供。
Expando 是 Model 的子类,并继承该类的类和实例方法。Expando 类不定义或重写任何方法。
简介
Expando 模型可以有固定属性和动态属性。固定属性的行为类似于 Model 的属性,并且使用类属性在 Expando 模型类中进行类似定义。当在实例上为动态属性分配值时,系统将创建动态属性。同一 Expando 类的两个实例可拥有不同的动态属性组合,甚至可以有名称相同但类型不同的动态属性。动态属性始终是可选的且没有默认值:直到为这些属性分配值时,它们才存在。
动态属性不能使用 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 类,其属性在使用前不需要在类中定义。与 Model 类似,必须对 Expando 类进行子类化才能定义数据实体的种类。
Expando 是 Model 的子类,并继承或重写其方法。
参数
- parent
- 新实体的父实体的 Model 实例或 Key 实例。
- key_name
-
新实体的名称。该名称会成为主键的一部分。如果此参数为 None
,则系统生成的 ID 将用于该键。
key_name 的值不得以数字开头,也不得采用 __*__
格式。如果您的应用使用用户提交的数据(例如电子邮件地址)作为数据存储区实体键名称,则应用应首先清理该值,例如在其前面添加一个已知字符串(如“key:”),以满足这些要求。
key_name
存储为 Unicode 字符串,str
值转换为 ASCII 文本。
- **kwds
- 实例属性的初始值,作为关键字参数。每个名称与新实例的属性对应,并可能与 Expando 类中定义的固定属性对应,或与动态属性对应。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]