# Common Tasks and How to Do Them in Python - Google App Engine - Google Code

English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# Common Tasks and How to Do Them in Python

- How do I update information in my datastore?
- Can I do common MySQL-like queries on the datastore?
- What third party libraries can I use in my application?
- Can I use Django with Google App Engine?
- Can I import my own version of a module that is included with Google App Engine?
- How do I profile my app's performance?
- When I run Django, I'm getting an error stating that ROOT_URLCONF is not found.
- I'd like to map my app to http://myurl.com (also known as a naked domain).

## How do I update information in my datastore?

The put() operation for datastore models allows you to both add a new entity to your datastore, and update existing datastore entities.

For instance, let's say we wanted users of a guestbook to have the ability to update their guestbook comments. We can retrieve the existing comment in our datastore, replace the existing text with new text, and commit the change to our datastore as follows:

    # For reference, our comment model might look like this
    class Comment(db.Model):
        comment_author = db.UserProperty()
        comment_text = db.TextProperty()

    class UpdateComment(BaseRequestHandler):
        def post(self):
            # Assume that receive an HTTP post with the updated comment in the comment field
            updated_comment = self.request.get('comment')
            current_user = users.GetCurrentUser()
            # Form the query to retrieve the comment
            comment_query = Comment.gql('WHERE comment_author = :1', current_user)
            # Fetch the result from the datastore
            comment_result = comment_query.fetch(1)
            if comment_result:
                comment = comment_result[0]
            else:
                logging.error('Could not find comment')

            # Assign the updated comment to the comment text
            comment.comment_text = db.Text(updated_comment)
            # Put the updated entity in the datastore
            comment.put()

## Can I do common MySQL-like queries on the datastore?

Google's App Engine datastore uses a non-relational object model to store information, which allows you to create fast, scalable applications. Because this method of storing data differs from relational databases, such as MySQL, not all functionalities used with relational databases are available with the system. For instance, the Google App Engine datastore does not have the ability to 'join' tables that is available in a relational database.

We have full CRUD operation (create, retrieve, update and delete) support, and allow you to query against our datastore using GQL, our query language. You may also use our Query class, which allows you to use filters and ordering when retrieving the data, as well as perform ancestors queries for an entity.

Datastore filters allow you to restrict the data retrieved from the datastore by using common comparison operators: `< <= = >= >`

The ordering operations allow you to organize how the data is returned from the query. You can view the ordering operations for each type available with the datastore here.

Lastly, when retrieving the data from the datastore, you can specify both a limit and an offset to the results returned. The limit is maximum number of results returned by the query. The fetch offset specifies the number of results to skip when returning the data from the datastore.

## What third party libraries can I use in my application?

You can use any pure Python third party libraries in your Google App Engine application. In order to use a third party library, simply include the files in your application's directory, and they will be uploaded with your application when you deploy it to our system. You can import the files as you would any other Python files with your application.

## Can I use Django with Google App Engine?

Absolutely. We've put together an introductory article explaining how to get started with Django and Google App Engine.

## Can I import my own version of a module that is included with Google App Engine?

If you would like to import your own version of a module included with Google App Engine, you will need to follow a few simple steps to get your import working correctly. First, include the files for the module that you wish to import in your application's directory.

Before importing the modules in your application's code, you must delete all references to the existing module in sys.modules, and then move your application's directory to the front of the sys.path so that your version gets imported. Below, we give an example of how you would import a different version of Django than the one included with Google App Engine:

    # Delete the preloaded copy of Django
    for key in [key for key in sys.modules if key.startswith('django')]:
      del sys.modules[key]

To move your application's directory to the front of the sys.path:

    # Force sys.path to have our own directory first, so we can import from it
    sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

## How do I profile my app's performance?

To profile your application's performance, first rename your application's `main()` function to `real_main()`. Then, add a new main function to your application, named `profile_main()`such as the one below:

    def profile_main():
        # This is the main function for profiling
        # We've renamed our original main() above to real_main()
        import cProfile, pstats
        prof = cProfile.Profile()
        prof = prof.runctx("real_main()", globals(), locals())
        print "<pre>"
        stats = pstats.Stats(prof)
        stats.sort_stats("time")  # Or cumulative
        stats.print_stats(80)  # 80 = how many to print
        # The rest is optional.
        # stats.print_callees()
        # stats.print_callers()
        print "</pre>"

This function will append the profile output to your HTML response. If instead you prefer to log the output, you can modify the profiler's main function as such:

    def profile_main():
        # This is the main function for profiling
        # We've renamed our original main() above to real_main()
        import cProfile, pstats, StringIO
        prof = cProfile.Profile()
        prof = prof.runctx("real_main()", globals(), locals())
        stream = StringIO.StringIO()
        stats = pstats.Stats(prof, stream=stream)
        stats.sort_stats("time")  # Or cumulative
        stats.print_stats(80)  # 80 = how many to print
        # The rest is optional.
        # stats.print_callees()
        # stats.print_callers()
        logging.info("Profile data:\n%s", stream.getvalue())

The log message will be rather verbose, and viewable in your Admin Console. For more information on logs, please see our article located here.

To enable the profiling with your application, set `main = profile_main`. To run your application as normal, simply set `main = real_main`.

(Note that cProfile was added in Python 2.5, which I recommend using since this is the version that App Engine uses to run your application. If you are using an earlier version of Python, you will not be able to do profiling locally.)

## When I run Django, I'm getting an error stating that ROOT_URLCONF is not found.

Since Google App Engine's webapp framework uses Django templates, Django will half-initialize when webapp is loaded. This causes the initialization of the rest of Django's setting to be skipped. If you are getting this error, you need to explicitly force Django to reload your settings:

    from django.conf import settings
    settings._target = None

## I'd like to map my app to http://myurl.com (also known as a naked domain).

This question has moved to the general FAQ page: code.google.com/appengine/kb/general.html#naked_domain.