zerowords
zerowords

Reputation: 3123

ReferenceProperty examples in google docs are too terse for me

I am trying to reproduce the examples at this link. I have concocted a few examples to try to understand them. I would appreciate any guidance on how to make my examples work. You may recall the quote below which I think is what the examples are attempting to portray.

ReferenceProperty automatically references and dereferences model instances as property values: a model instance can be assigned to a reference property directly, and its key will be used.

Update below

In my question, I wanted clarification and examples for the 4 lines of code in the documents that are copied just below.

story = db.get(story_key)
author_name = story.author.name

author = db.get(author_key)
stories_by_author = author.story_set.get()

But that code is irrelevant for the ndb model which all replying folks seem to insist I should be using instead of db. Since I don't know how to code for ndb, and could not reproduce the db results, I could not envision how ReferenceProperty or now KeyProperty works from the documentation. I believe I have gotten answers for all 4 lines of code, and will present that here. If someone can confirm my answers to the 4 examples, I will be very happy.

So, my 4 examples just below are written for ndb now, not db.

story = ja4.get()         #line 1 (Story(key=Key('Story', 5294973121462272), author=Key('Author', 5857923074883584), pov=u'p4'))
story.author.get().name   #line 2 (a1)
story.author.get()        #line 3 (Author(key=Key('Author',5857923074883584), name=u'a1'))
for astory in Story.query(Story.author == story.author.get().key):
     print astory.pov     #line 4 (p1, p2, p4)

Update above

class Author(db.Model):
    name = db.StringProperty()

class Story(db.Model):
    author = db.KeyProperty(Author)  #not Reference

story = db.get(story_key)    #I can make this work, but no more.
author_name = story.author.name   #Errors are listed below.

author = db.get(author_key)
stories_by_author = author.story_set.get()

Below is my test data and some trial code.

class Author(ndb.Model):
    name = ndb.StringProperty()

class Story(ndb.Model):
    author = ndb.KeyProperty(Author)
    pov  = ndb.StringProperty()

author1 = Author(name='a1')
author2 = Author(name='a2')
author3 = Author(name='a3')
ka1 = author1.put()
ka2 = author2.put()
ka3 = author3.put()

story1 = Story(pov='p1', author=ka1)
story2 = Story(pov='p2', author=ka2)
story3 = Story(pov='p3', author=ka1)
story4 = Story(pov='p4', author=ka1)
story5 = Story(pov='p5', author=ka2)
story6 = Story(pov='p6', author=ka3)

ja1 = story1.put()
ja2 = story2.put()
ja3 = story3.put()
ja4 = story4.put()
ja5 = story5.put()
ja6 = story6.put()

Upvotes: 0

Views: 49

Answers (2)

zerowords
zerowords

Reputation: 3123

Using the ndb Models and data in the question, these next 4 "lines" answer my question.

story = ja4.get()         #line 1 (Story(key=Key('Story', 5294973121462272), author=Key('Author', 5857923074883584), pov=u'p4'))
story.author.get().name   #line 2 (a1)
story.author.get()        #line 3 (Author(key=Key('Author',5857923074883584), name=u'a1'))
for astory in Story.query(Story.author == story.author.get().key):
     print astory.pov     #line 4 (p1, p2, p4)

Upvotes: 0

GAEfan
GAEfan

Reputation: 11370

Here is a non-answer answer:

Do yourself a favor and use the newer ndb over db. There, the syntax would be:

from google.appengine.ext import ndb

class Author(ndb.Model):
    name    = ndb.StringProperty()

class Story(ndb.Model):
    author  = ndb.KeyProperty(kind = Author)
    pov     = ndb.StringProperty()

Upvotes: 3

Related Questions