Chameleon
Chameleon

Reputation: 10138

How to get related to db.Model entity from datastore - what API allows it?

I want to read related to model entity. What API I should use?

For example:

class DeleteMe(db.Model):
  x = db.FloatProperty()

DeleteMe(key_name = '1').put()

How to read raw entity from datastore for key_name = '1'?

Upvotes: 0

Views: 103

Answers (1)

jeremydw
jeremydw

Reputation: 74

To get the corresponding model that you just put, use get_by_key_name. (https://developers.google.com/appengine/docs/python/datastore/modelclass#Model_get_by_key_name)

DeleteMe.get_by_key_name('1')

However, I noticed you're using the db package and not ndb. I would encourage you to use ndb as it has many optimizations and a more powerful API to the datastore.

https://developers.google.com/appengine/docs/python/ndb/

Corresponding code for NDB might look like:

from google.appengine.ext import ndb

class DeleteMe(ndb.Model):
  x = ndb.FloatProperty()

DeleteMe(id='1').put()
DeleteMe.get_by_id('1')

Upvotes: 1

Related Questions