Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
元数据实体 Python 参考
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:强烈建议构建新应用的开发者使用 NDB 客户端库,它与 DB 客户端库相比具有多项优势,例如可通过 Memcache API 进行自动实体缓存。如果您当前使用的是较早的 DB 客户端库,请参阅 DB 到 NDB 的迁移指南
模型类 EntityGroup
、Namespace
、Kind
和 Property
表示获取或查询元数据种类的结果。这些类均在 google.appengine.ext.db.metadata
软件包中定义。如需了解详情及其使用示例,请参阅元数据页面。
辅助函数
以下辅助函数可用于获取元数据信息,无需发出显式查询或 get 调用:
-
get_entity_group_version(entity_or_key)
-
返回包含 entity_or_key
的实体组的版本。此版本必定随实体组的每次更改而提高。即使实体组没有用户可见的更改时,版本也可能会提高。如果实体组从未被写入,可能会返回 None
。
警告:实体组元数据的行为现在有所不同。无论用户是否执行了更新,实体组版本都可能会提高。以前,实体组版本仅在用户执行更新时提高。要保持旧版行为,请使用实体组版本解决方法。
参数:
- entity_or_key
- 键或您想要其版本的实体。
-
get_namespaces(start=None, end=None)
-
返回命名空间名称列表。
参数
-
start
-
如果不是 None
,则仅返回大于或等于指定值的命名空间名称。
-
end
-
如果不是 None
,则仅返回小于指定值的命名空间名称。
-
get_kinds(start=None, end=None)
-
返回实体种类名称列表。
参数
-
start
-
如果不是 None
,则仅返回大于或等于指定值的种类名称。
-
end
-
如果不是 None
,则仅返回小于指定值的种类名称。
-
get_properties_of_kind(kind, start=None, end=None)
-
返回指定种类的索引属性名称列表。不包括未编入索引的属性。
参数
-
kind
-
属性名称被请求的种类。
-
start
-
如果不是 None
,则仅返回大于或等于指定值的属性名称。
-
end
-
如果不是 None
,则仅返回小于指定值的属性名称。
-
get_representations_of_kind(kind, start=None, end=None)
-
返回一个字典,将指定种类的每个编入索引的属性名称与其表示法列表相映射。不包括未编入索引的属性。
参数
-
kind
-
属性表示法被请求的种类。
-
start
-
如果不是 None
,则仅返回名称大于或等于指定值的属性的表示法。
-
end
-
如果不是 None
,则仅返回名称小于指定值的属性的表示法。
EntityGroup 类
__entity_group__
元数据的模型(仅适用于 HR 数据存储区)。
此元数据包含数字 __version__
属性,该属性必定随实体组的每次更改而提高。即使实体组没有用户可见的更改时,版本也可能会提高。如果实体组从未被写入,则 __entity_group__
实体可能不存在。
属性
- version
- 版本号
类方法
- key_for_entity(entity_or_key)
- 返回包含
entity_or_key
的实体组的元数据键。使用此键作为调用 get()
的参数,以获取包含 entity_or_key 的实体组的 __entity_group__
元数据实体。参数:
- entity_or_key
- 键或您想要其
__entity_group__
版本的实体。
Namespace 类
Namespace
类表示特殊种类 __namespace__
的元数据实体,作为命名空间查询结果返回。此实体的键是命名空间的名称。(由空字符串 ""
指定的默认命名空间例外:由于空字符串不是有效的键名,因此此命名空间改为以数字 ID 1
为键)。
属性
Namespace
类的实例具有以下属性:
-
namespace_name
-
返回由该实体的键指定的命名空间的名称。
类方法
Namespace
类具有以下类方法:
-
key_for_namespace(namespace)
-
返回用于指定命名空间的 __namespace__
键。
参数
-
namespace
-
请求其键的命名空间。
-
key_to_namespace(key)
-
返回由指定的 __namespace__
键指定的命名空间。
参数
-
key
-
请求其名称的键。
Kind 类
Kind
类表示特殊种类 __kind__
的元数据实体,作为种类查询结果返回。该实体的键是实体种类的名称。
属性
Kind
类的实例具有以下属性:
-
kind_name
-
返回该实体的键指定的种类名称。
类方法
Kind
类具有以下类方法:
-
key_for_kind(kind)
-
返回指定类型的 __kind__
键。
参数
-
kind
-
请求其键的种类。
-
key_to_kind(key)
-
返回由指定的 __kind__
键指定的类型。
参数
-
key
-
请求其种类的键。
Property 类
Property
类表示特殊种类 __property__
的元数据实体,作为属性查询结果返回。代表种类 K 的属性 P 的实体按以下方式构建:
- 实体的键具有种类
__property__
和键名 P。
- 父实体的键具有种类
__kind__
和键名 K。
属性
Property
类的实例具有以下属性:
-
kind_name
-
返回该实体的键指定的种类名称。
-
property_name
-
返回该实体的键指定的属性名称。
类方法
Property
类具有以下类方法:
-
key_for_kind(kind)
-
返回指定类型的 __property__
键的父键。
参数
-
kind
-
请求其父键的种类。
-
key_for_property(kind, property)
-
返回指定属性和种类的 __property__
键。
参数
-
kind
-
请求其键的种类。
-
property
-
请求其键的属性。
-
key_to_kind(key)
-
返回由指定的 __property__
键指定的类型。
参数
-
key
-
请求其种类名称的键。
-
key_to_property(key)
-
返回由指定的 __property__
键指定的属性,或者返回 None
(如果该键仅指定一种类型)。
参数
-
key
-
请求其属性名称的键。
如未另行说明,那么本页面中的内容已根据知识共享署名 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 use the NDB Client Library for new applications due to its advantages, such as automatic entity caching, over the older DB Client Library.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egoogle.appengine.ext.db.metadata\u003c/code\u003e package contains model classes like EntityGroup, Namespace, Kind, and Property, which represent the results of metadata queries.\u003c/p\u003e\n"],["\u003cp\u003eHelper functions are available to get metadata without explicit queries, including \u003ccode\u003eget_entity_group_version\u003c/code\u003e, \u003ccode\u003eget_namespaces\u003c/code\u003e, \u003ccode\u003eget_kinds\u003c/code\u003e, \u003ccode\u003eget_properties_of_kind\u003c/code\u003e, and \u003ccode\u003eget_representations_of_kind\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eget_entity_group_version\u003c/code\u003e function returns the version of an entity group, which now may increase even without user updates, a change from its previous behavior.\u003c/p\u003e\n"],["\u003cp\u003eFunctions such as \u003ccode\u003eget_namespaces\u003c/code\u003e, \u003ccode\u003eget_kinds\u003c/code\u003e, \u003ccode\u003eget_properties_of_kind\u003c/code\u003e, and \u003ccode\u003eget_representations_of_kind\u003c/code\u003e allow to filter their results based on start and end values provided.\u003c/p\u003e\n"]]],[],null,["# Metadata Entities Python Reference\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 model classes\n[EntityGroup](#EntityGroup),\n[Namespace](#The_Namespace_Class),\n[Kind](#The_Kind_Class),\nand\n[Property](#The_Property_Class)\nrepresent the results of getting or querying metadata kinds.\nThese classes are all defined in the package\n`google.appengine.ext.db.metadata`. See the page\n[Metadata](/appengine/docs/legacy/standard/python/datastore/metadataqueries)\nfor more information and examples of their use.\n\nHelper Functions\n----------------\n\nThe following helper functions are available for obtaining metadata\ninformation without issuing an explicit query or get:\n\n\nget_entity_group_version(entity_or_key)\n\n: Returns the version of the entity group containing\n `entity_or_key`. This\n version is guaranteed to increase on every change to the entity\n group. The version may increase even in the absence of user-visible\n changes to the entity group.\n May return `None` if the entity group was\n never written to.\n\n **Warning** : The behavior of entity group metadata is now different. The entity\n group version can increase independently of user updates. Previously, the entity\n group version increased only on user update. To maintain legacy behavior, use the\n [entity group version workaround](https://support.google.com/cloud/answer/9133787#zippy=python-db).\n\n **Arguments:**\n\n entity_or_key\n : a key or entity whose version you want.\n\n\nget_namespaces(start=None, end=None)\n\n: Returns a list of namespace names.\n\n Arguments\n\n\n start\n\n : If not `None`, return only namespace names greater than or equal to the specified value.\n\n\n end\n\n : If not `None`, return only namespace names less than the specified value.\n\n\nget_kinds(start=None, end=None)\n\n: Returns a list of entity kind names.\n\n Arguments\n\n\n start\n\n : If not `None`, return only kind names greater than or equal to the specified value.\n\n\n end\n\n : If not `None`, return only kind names less than the specified value.\n\n\nget_properties_of_kind(kind, start=None, end=None)\n\n: Returns a list of indexed property names for a specified kind. Unindexed properties are not included.\n\n Arguments\n\n\n kind\n\n : The kind whose property names are requested.\n\n\n start\n\n : If not `None`, return only property names greater than or equal to the specified value.\n\n\n end\n\n : If not `None`, return only property names less than the specified value.\n\n\nget_representations_of_kind(kind, start=None, end=None)\n\n: Returns a dictionary mapping each indexed property name for a specified kind to a list of its representations. Unindexed properties are not included.\n\n Arguments\n\n\n kind\n\n : The kind whose property representations are requested.\n\n\n start\n\n : If not `None`, return only representations for properties whose names are greater than or equal to the specified value.\n\n\n end\n\n : If not `None`, return only representations for properties whose names are less than the specified value."]]