NDB for beginner's to get a start.
NDB is the newest way to store data in GAE(Google AppEngine). I had to struggle with it to get a good start here is what i learned from my experiments.
In the above example, we have to declare entity property(like column in mysql) and data type or property
e.g. ndb.StringProrperty(required=True)
There are more type, follow the link for this.
Like i have passed the option that this property can't be empty using required = True.
There are more for this follow the link. These called property options.
Note: These property can be accessed by shorter name.
e.g
Note: The link has advanced part you can skip for now link.
Put data into the table(ndb.Model).
case 1
There is no need to put parent key you can do this directly
case 2
you can get the entity in
case 2:
As
for the case 1:
Alternate:
For Google cheat sheet.follow the link
For the cursor and pagination use the following . here form the same cheat sheet.
a= holds the data here and
cur = cursor for the next data
more = true if more data is there.
Note: for the next data and cursor : use
start_cursor = cur
for the previous data and cursors
end_cursor = cur
Note: this tutorial is for GAE using Python.
Here tables are represented by class and class name represents table(kind).
These class has to inherit form ndb.Model.
for this.
here is the code
create file with the two kinds
from google.appengine.ext import ndbSave this file named as models.py .
class Religion(ndb.Model):
religion = ndb.StringProperty(required=True)
no_of_people = ndb.IntegerProperty()
name = ndb.StringProperty()
class Student(ndb.Model):
name = ndb.StringProperty()
age = ndb.StringProperty()
religion = ndb.StringProperty()
In the above example, we have to declare entity property(like column in mysql) and data type or property
e.g. ndb.StringProrperty(required=True)
There are more type, follow the link for this.
Like i have passed the option that this property can't be empty using required = True.
There are more for this follow the link. These called property options.
Note: These property can be accessed by shorter name.
e.g
first_name_student = ndb.StringProperty('f_name')Now, the property can be accessed using f_name too.
Note: The link has advanced part you can skip for now link.
Put data into the table(ndb.Model).
case 1
student_key = ndb.Key(Religion,"brotherhood")Here i have used parent key that links it the entity in the kind Religion with id="brotherhood".
student = Student(parent = student_key)
student.age = age
student.id = "Global"
student.religion = "brotherhood"
student.name = "human"
student.put() #this is put data into the entity table.
There is no need to put parent key you can do this directly
case 2
student = Student(parent = student_key)Get entity:
student.age = age
student.id = "Global"
student.religion = "brotherhood"
student.name = "human"
student.put() #this is put data into the entity table.
you can get the entity in
case 2:
As
entity = Student.get_by_id(id_value)not id_value can be get as key = student.put(); id_value = key.id() # this gives the id.
for the case 1:
entity_value = Student.get_by_id(id_value, parent=ndb.Key(models.Religion,"brotherhood")If have used parent key then we have to supply the parent key to get the entity.
Alternate:
key = student.put()Get the parent entity.
id = key.id()
parent_key = key.parent()
self.response.out.write("=======####-----------<br/>")For more on the url_safe. follow the link.
self.response.out.write(key.parent().id())
self.response.out.write("<br/>")
self.response.out.write(key.parent().urlsafe())
self.response.out.write("<br/>")
self.response.out.write(key.parent())
self.response.out.write("<br/>")
self.response.out.write(key.parent().get())
For Google cheat sheet.follow the link
google.appengine.ext.db | ndb.model |
q = MyModel.all() q.ancestor(ancestor_key) | q = MyModel.query(ancestor=ancestor_key) |
q = MyModel.all(keys_only=True) r = q.fetch(N) | r = MyModel.query() \ .fetch(N, keys_only=True) # Alternatively: q = MyModel.query( default_options=QueryOptions( keys_only=True)) r = q.fetch(N) |
For the cursor and pagination use the following . here form the same cheat sheet.
Cursors
q = MyModel.all() a = q.fetch(20) cur = q.cursor() | q = MyModel.query() a, cur, more = q.fetch_page(20) # (1) |
q.with_cursor(cur) b = q.fetch(20) | b, cur, more = \ q.fetch_page(20, start_cursor=cur) |
q.with_cursor(end_cursor=cur) b = q.fetch(20) | q.fetch(20, end_cursor=cur) |
a= holds the data here and
cur = cursor for the next data
more = true if more data is there.
Note: for the next data and cursor : use
start_cursor = cur
for the previous data and cursors
end_cursor = cur
list_of_keys = ndb.put_multi(list_of_entities)
list_of_entities = ndb.get_multi(list_of_keys)
ndb.delete_multi(list_of_keys)
For, now this is ok! next we will look into the id's some link.
from google.appengine.api import namespace_manager
namespace_manager.set_namespace('thenamespace')
MyModel.get_by_id(some_id_name)
http://stackoverflow.com/questions/10321244/cant-use-ndb-get-by-id-to-get-entity-in-namespace