I am back into AppEngine development.
And I am quite hang up with the Datastorage.
I am used to “ids”, building the website with links like “/view?id=322345” gets me the GuestBook entry with this id voilà.
But in AppEngine I always see the db.Key.from_path() method called and other stuff like “Ancestors” and “parents”.
In my project I have simple relationships like User->Phonenumbers, and I want to inspect each User via a GET url appending something id-like.
Should I use the User models key() or how is this in AppEngine achieved?
Currently if I want to add i.e. a PhoneNumber to the User I write the following:
class JoinHandler(webapp2.RequestHandler):
def get(self):
user_key = self.request.get('rlm')
fon_number = PhoneNumber()
fon_number.country = "deDE"
fon_number.user = db.Key(user_key)
fon_number.put()
self.redirect("/")
If you are using the numeric ids that are automatically assigned by the datastore when you
putan entity, you can then retrieve the entity with the model’sget_by_idmethod:Now, if you also want to store phone number entities that belong to a given user, you make store them such that their parent is the User entity. This puts them in the User’s entity group and entities that are in an entity group can be queried together more quickly than if they are stored with parents.
Let’s say that any User can have any given number of Phonenumber entities associated with it:
We’ll add a method to the User class:
Obviously, these classes are naive and straight off the top of my head, but I hope that they’ll help you understand both id’s and ancestor relationships. Often times, you won’t have to use
Key()to manually create keys at all. Don’t go that route unless you really need to. Still, understanding how Keys work is good for really understanding AppEngine, so dive in and experiment if you feel like it.I think that those were your two main questions, yeah? If you have others, go ahead and leave a comment, and I’ll edit my answer.