如果使用与有效 Google 账号不对应的电子邮件地址调用 User 构造函数,则会创建对象,但该对象与实际的 Google 账号不对应。
即使有人在存储了该对象后用指定的电子邮件地址创建了 Google 账号,也将出现这种情况。如果创建 User 值时使用的电子邮件地址当时不代表 Google 账号,该 User 值将永远不会与代表真实用户的 User 值相匹配。
当在开发 Web 服务器下运行时,所有 User 对象在存储于(模拟)Datastore 中时都假定代表有效的 Google 账号。
有效用户的 User 对象可以为该用户提供唯一 ID 值,即使该用户更改了其电子邮件地址,该 ID 值也保持不变。user_id() 方法会返回此 ID,这是一个 str 值。
无论您的应用使用哪种身份验证方法,User 对象均采用相同的形式。
将 User 值与 Datastore 配合使用
用户 ID 是稳定的;您可以在键名或字符串属性中使用。
因此,当使用用户值时,您需要存储用户 ID(或许是最后一次看到的邮件地址,以便通过邮件与用户通信)。以下示例展示了如何将当前用户与用户 ID 进行比较:
[[["易于理解","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\u003eThe \u003ccode\u003eUser\u003c/code\u003e class represents a user and provides unique, comparable instances, where equal instances represent the same user.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eusers.get_current_user()\u003c/code\u003e function retrieves the \u003ccode\u003eUser\u003c/code\u003e instance for the currently authenticated user, regardless of the authentication method.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eUser\u003c/code\u003e instances can be created using an email address or a federated identity, but those created from invalid Google accounts will not match real user accounts.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003euser_id()\u003c/code\u003e method returns a stable, unique string identifier for a user that persists even if the user changes their email.\u003c/p\u003e\n"],["\u003cp\u003eIt is recommended to store the \u003ccode\u003euser_id\u003c/code\u003e for user identification in the datastore instead of a \u003ccode\u003eUserProperty\u003c/code\u003e because email addresses may change.\u003c/p\u003e\n"]]],[],null,["# User Objects\n\nAn instance of the\n[User](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.users#google.appengine.api.users.User)\nclass represents a user. User instances are unique and comparable. If two instances are equal,\nthen they represent the same user.\n| This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nThe application can access the User instance for the current user by calling the\n[users.get_current_user()](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/users#get_current_user)\nfunction. \n\n user = users.get_current_user()\n\nYou can use the [users.get_current_user()](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/users#get_current_user)\nfunction no matter which authentication option your app uses.\n\nA User instance can be also constructed from an email address: \n\n```python\nuser = users.User(\"Albert.Johnson@example.com\")\n```\n\nOr, if you have a `federated_identity`, you can use it to create a User instance: \n\n```python\nuser = users.User(federated_identity=\"http://example.com/id/ajohnson\")\n```\n\nIf the User constructor is called with an email address that does not correspond with a valid\nGoogle account, the object will be created but it will not correspond with a real Google account.\nThis will be the case even if someone creates a Google account with the given email address after\nthe object is stored. A User value with an email address that does not represent a Google account\nat the time it is created will never match a User value that represents a real user.\n\nWhen running under the development web server, all User objects are assumed to represent valid\nGoogle accounts when stored in the (simulated) datastore.\n\nThe User object for a valid user can provide a unique ID value for the user that stays the same\neven if the user changes her email address. The `user_id()` method returns this ID, a\n`str` value.\n\nThe User object has the same form no matter which method of authentication your app uses.\n\nUsing User Values With the Datastore\n------------------------------------\n\nThe user ID is stable; you can use it in a key name or as a string property.\nTherefore, when using user values, you want to store the user ID (and perhaps the last-seen\nmail address to communicate with the user by mail). The example below shows how to compare the\ncurrent user to a user ID: \n\n class ModelWithUser(ndb.Model):\n user_id = ndb.StringProperty()\n color = ndb.StringProperty()\n\n @classmethod\n def get_by_user(cls, user):\n return cls.query().filter(cls.user_id == user.user_id()).get()\n\nWe strongly recommend that you do not store a `UserProperty`, because it includes the email address\nalong with the user's unique ID. If a user changes their email address and you compare their old, stored\n`User` to the new `User` value, they won't match. Instead, consider using the\n`User` *user ID value* as the user's stable unique identifier."]]