Ian Burris
Ian Burris

Reputation: 6515

Selecting data from Google App Engine datastore by field value

I'm just staring off with GAE. So like many I'm used to standard SQL.

Typically when you want to select data that has a certain field value you use:

SELECT <columns> FROM <table> WHERE <column> = <wanted value>

Is the correct way to do this in GAE

<Model Class>.all().filter('<column> =', <wanted value>)

Or is there a more efficient way?

EDIT: Also I should note in this particular case I only want one result returned. So is there another command so that it doesn't keep looking after if finds a result?

Upvotes: 4

Views: 592

Answers (2)

David Underhill
David Underhill

Reputation: 16233

Your code is pretty close to what you're looking for - it constructs a Query object which can be used to query the datastore. To actually get a result, you'll need to execute the query. To get a single result, you'll want to use the get() method:

result = <Model Class>.all().filter('<column> =', <wanted value>).get()

Upvotes: 6

msw
msw

Reputation: 43487

You probably want Model.gql('where column = :value', value=something) which returns a GqlQuery upon which a GqlQuery.get() returns a single item.

Upvotes: 1

Related Questions