Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

User Objects

An instance of theUser class represents a user. User instances are unique and comparable. If two instances are equal, then they represent the same user.

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 themigration guide to learn about your migration options for legacy bundled services.

The application can access the User instance for the current user by calling theusers.get_current_user() function.

user=users.get_current_user()

You can use theusers.get_current_user() function no matter which authentication option your app uses.

A User instance can be also constructed from an email address:

user=users.User("Albert.Johnson@example.com")

Or, if you have afederated_identity, you can use it to create a User instance:

user=users.User(federated_identity="http://example.com/id/ajohnson")

If the User constructor is called with an email address that does not correspond with a valid Google account, the object will be created but it will not correspond with a real Google account. This will be the case even if someone creates a Google account with the given email address after the object is stored. A User value with an email address that does not represent a Google account at the time it is created will never match a User value that represents a real user.

When running under the development web server, all User objects are assumed to represent valid Google accounts when stored in the (simulated) datastore.

The User object for a valid user can provide a unique ID value for the user that stays the same even if the user changes her email address. Theuser_id() method returns this ID, astr value.

The User object has the same form no matter which method of authentication your app uses.

Using User Values With the Datastore

The user ID is stable; you can use it in a key name or as a string property.Therefore, when using user values, you want to store the user ID (and perhaps the last-seenmail address to communicate with the user by mail). The example below shows how to compare the current user to a user ID:

classModelWithUser(ndb.Model):user_id=ndb.StringProperty()color=ndb.StringProperty()@classmethoddefget_by_user(cls,user):returncls.query().filter(cls.user_id==user.user_id()).get()

We strongly recommend that you do not store aUserProperty, because it includes the email addressalong with the user's unique ID. If a user changes their email address and you compare their old, storedUser to the newUser value, they won't match. Instead, consider using theUseruser ID value as the user's stable unique identifier.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.