wombatonfire
wombatonfire

Reputation: 5410

Cursor returns with fetch() but not run() on App Engine

I have this python code for app engine:

def queryArticles(status=1, limit=10, **offset):
    query = Article.all().filter('status =', status).order('-modified')
    articles = query.fetch(limit=limit, **offset)
    cursor = query.cursor()
    return articles, cursor

If I use fetch() cursor returns, but in case run() is used I get empty string for cursor. Why?

Upvotes: 1

Views: 332

Answers (2)

Igor Kharin
Igor Kharin

Reputation: 699

Query.run() (or simply iterating over Query's instance) returns an iterator. At the time you getting the query's cursor it hasn't even started fetching anything. I guess, in your case — as you going to use cursors — fetch() would be just fine.

In case you really need it, this should do the trick:

def queryArticles(status=1, limit=10, **offset):
    query = Article.all().filter('status =', status).order('-modified')
    articles = query.run(limit=limit, batch_size=limit, **offset)
    articles = articles.next()
    cursor = query.cursor()
    return articles, cursor

Upvotes: 1

tdavis
tdavis

Reputation: 1472

The run() method merely sets up the conditions required to run the query (and in production, starts the RPC call in the background). In order for your query to have any knowledge of data (cursors or otherwise), you need to fetch the results by exhausting the query.

>>> query = Model.all()
>>> query_iterator = query.run(limit=5)
>>> query.cursor()
''
>>> list(query_iterator) # Exhaust iterator
[<db.Model instance at 0x...>, ...]
>>> query.cursor()
'E-ABAIICG2oMYXByaWdvbmluamE0cgsLEgVNb2RlbBgKDBQ='

Upvotes: 3

Related Questions