niteshb
niteshb

Reputation: 2839

How to retrieve entity from key value in GQL

I am using Google App Engine's datastore and wants to retrieve an entity whose key value is written as

ID/Name

id=1

Can anyone suggest me a GQL query to view that entity in datastore admin console and also in my python program?

Upvotes: 5

Views: 2409

Answers (2)

systempuntoout
systempuntoout

Reputation: 74134

From your application use the get_by_id() class method of the Model:

entity = YourModel.get_by_id(1)

From Datastore viewer you should use the KEY function:

SELECT * FROM YourModel WHERE __key__ = KEY('YourModel',1)

Upvotes: 5

garnertb
garnertb

Reputation: 9594

An application can retrieve a model instance for a given Key using the get() function.

class member(db.Model):
    firstName=db.StringProperty(verbose_name='First Name',required=False)
    lastName=db.StringProperty(verbose_name='Last Name',required=False)

...

id = int(self.request.get('id'))
entity= member.get(db.Key.from_path('member', id))

I'm not sure how to return a specific entity in the admin console.

Upvotes: 0

Related Questions