1

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.

starter
  • 169
  • 2
  • 10
  • I've tried to do little otherwise. But, I still get the same error. Below is my new code: k = db.Key.from_path('News', 4264) news = db.GqlQuery("SELECT * FROM News WHERE key =:1", k) news[0].title – starter May 17 '11 at 23:08
  • You have a few pretty basic errors, you should probably familiarise yourself with [GQL](http://code.google.com/appengine/docs/python/datastore/gqlreference.html) and the various [handy methods on Model](http://code.google.com/appengine/docs/python/datastore/modelclass.html#Model_get_by_id) – Chris Farmiloe May 17 '11 at 23:22

2 Answers2

1

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))
...
hyperslug
  • 3,473
  • 1
  • 28
  • 29
1

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))

Abdul Kader
  • 5,781
  • 4
  • 22
  • 40