Sunday, December 29, 2013

Google App Engine SDK for Python - Using datastore query at local for testing

When I tried to do some test of datastore query in the python console:

query = MyModel.all()
query.filter("username =", "myusername")
result = query.get()

I got this error"

BadArgumentError: app must not be empty.

After googling around, I found this solution: before execute the query, run these following commands:

import os

os.environ['APPLICATION_ID'] = 'dangtrinhnt'                               
datastore_file = '/dev/null'
from google.appengine.api import apiproxy_stub_map,datastore_file_stub
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()

stub = datastore_file_stub.DatastoreFileStub('dangtrinhnt', datastore_file, '/')
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

Then, execute any query I want, for example the query at the beginning of this blog post (the blue lines).