starter
starter

Reputation: 169

GqlQuery and ID

I try to build a GqlQuery and get an entity by the specified ID, but I get the error "IndexError: The query returned fewer than 1 results". A DataStore is not empty. At the Datastore Viewer I've found the necessary ID. My code is placed below:

from google.appengine.ext import db from models.models import News

news = News.gql("WHERE id = 4265")

print news[0].title


I know about the get_by_id method, by some reasons it is not satisfied me. But English is not native for me, because of this I didn't understand how to properly use Key.from_path.

Upvotes: 1

Views: 1603

Answers (2)

Abdul Kader
Abdul Kader

Reputation: 5842

Why do you want to write a Gql query, when you can just use get_by_id() method of Model Class.

note: id can be get from a object by object.Key().id() inside python and in templates you simply call object.key.id

news = News.get_by_id(4265)

or you can use db.Key.from_path to get the key and pass it to get() method.

new = News.get(db.Key.from_path('News', 4265))

Upvotes: 1

hyperslug
hyperslug

Reputation: 3623

Try:

news = News.get_by_id(4265)
if news is not None: 
  print news.title

Or if you must use GQL,

news = News.get(db.Key.from_path('News', 4265))
...

Upvotes: 1

Related Questions