0

I have pulled a list of entities from my datastore in app engine. Each entity in the list has a "name" attribute. Is there a quick way to search the list of entities for a specific name?

As opposed to iterating through each one and checking the name attribute

Thanks!

user1152327
  • 149
  • 12
  • are you using `db` or `ndb`? are names unique or are you searching for all entities with that name? – aschmid00 Sep 13 '12 at 17:22
  • names will be unique; not sure what you mean by db or ndb – user1152327 Sep 14 '12 at 01:49
  • There are two main GAE datastore access methods- DB and NDB. NDB is the newer one, use this. If names are unique in your app have a look at get_by_id() which will allow you, if you've used "name" as the ID of the model in the datastore, to retrieve the record directly. More here: https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_by_id – Paul Collingwood Sep 14 '12 at 09:07

1 Answers1

1

You can pull out the entity you are looking for directly if you have an attribute you can match against

q = Person.all()
q.filter("name =", target_last_name)
result = q.get()

But as far as I know, you have to iterate around the result list if you are not querying against a specific attribute.

This link talks about efficent ways to do that however:

Searching a list of objects in Python

E.G.

     [x for x in myList if x.n == 30]
Community
  • 1
  • 1
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36