mango
mango

Reputation: 553

In app engine, how does one get the key of an entity that has a certain attribute?

I want to check if an entity with a certain attribute value exists, and if it does, get the key of that entity.

Upvotes: 3

Views: 130

Answers (2)

Vincent
Vincent

Reputation: 1157

Will there be only one entity with that attribute value? Assuming only one entity can have this attribute value, you could try following:

matched_entity = Entity.all().filter('your_attribute =', Value)
if matched_entity.get():
    key = matched_entity.key()
    #your code for found item
else:
    #your code for item not found

Value can be anything like an integer, boolean, a string, or a variable defined before. It depends on the attribute type you have set in the model definition.

Upvotes: 1

Jerome
Jerome

Reputation: 146

It seems like what you want to do is a key only query: https://developers.google.com/appengine/docs/python/datastore/queries#Python_Keys_only_queries

You will have to make sure the "certain attributes" are indexed, such as you can query on them.

Upvotes: 1

Related Questions