John John - 23 days ago 10
Python Question

Retrieve an entity by entity id

In Datastore View, I can see

Edit Entity: SaleRecord
Decoded entity key: SaleRecord: id=4506503696351232

Entity key: ahJzfmF1dG90d2VldG1hY2hpbmVyFwsSClNhbGVSZWNvcmQYgICAwMLUgAgM


How can I retrieve that entity if I only know the key id (4506503696351232).
(I am using Python)

I tried:

s= SaleRecord.get('4506503696351232')


but it returned errors

Answer

You should be able to get it like this:

from google.appengine.ext import db

k = db.Key('ahJzfmF1dG90d2VldG1hY2hpbmVyFwsSClNhbGVSZWNvcmQYgICAwMLUgAgM')
s = db.get(k)

You can also create the key using the id, like this:

k = db.Key.from_path('SaleRecord', 4506503696351232)

Check the documentation for get and Key for more information.