Tutorial :Selecting data from Google App Engine datastore by field value



Question:

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?


Solution:1

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


Solution:2

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »