有効な Google アカウントに対応していないメールアドレスを使用して User コンストラクタが呼び出された場合、オブジェクトは作成されても実際の Google アカウントには対応しません。こうしたケースは、オブジェクトが保存された後に、特定のメールアドレスを使用して Google アカウントを作成する場合にも起こります。User 値の作成時に Google アカウントを表していないメールアドレスがその値に含まれていると、その User 値は実際のユーザーを表す User 値とは一致しません。
開発用ウェブサーバーで実行されている場合、すべての User オブジェクトは(シミュレートされた)データストアへの保存時には、有効な Google アカウントを表すと想定されます。
有効なユーザーの User オブジェクトにより、そのユーザーの一意の ID 値が得られます。この値はそのユーザーがメールアドレスを変更しても変わりません。この ID(str 値)は user_id() メソッドによって返されます。
User オブジェクトは、アプリでどの認証方式を使用する場合でも同じ形式になります。
データストアでユーザー値を使用する
ユーザー ID は不変であるため、キー名の中や文字列プロパティとして使用できます。そのため、ユーザー値を使用するときは、ユーザー ID(場合によってはユーザーとのメール通信で最後に確認したメールアドレス)を保存します。次の例は、現在のユーザーとユーザー ID を比較する方法を示しています。
UserProperty にはメールアドレスとユーザーの一意の ID が含まれているため、これを保存しないことを強くおすすめします。ユーザーがメールアドレスを変更すると、そのユーザーの古いメールアドレス(User に格納されている)と新しい User の値を比較したときに一致しません。代わりに、User のユーザー 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"]],["最終更新日 2025-09-04 UTC。"],[[["\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."]]