سؤال

Let me start by saying I'm really new to Python, app engine, and the datastore, so please be kind if I'm missing something obvious.

I'm trying to set up some data in the datastore with ancestor relationships, sort of the same way as in the example guestbook application from Google:

class Guestbook(webapp2.RequestHandler):
  def post(self):
    greeting = Greeting(parent=guestbook_key)

I created this code as an experiment to see if I could get it to work:

class Subscriber(ndb.Model):
    user_nickname = ndb.StringProperty()
    user_id = ndb.StringProperty()

class Music(ndb.Model):
    level = ndb.StringProperty() # top or sub
    parent = ndb.StringProperty() # only if sub
    title = ndb.StringProperty()

class SetupHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        subscriber = Subscriber(id=user.user_id())
        subscriber.user_id = user.user_id()
        subscriber.user_nickname = user.nickname()

        haydn = Music(parent=subscriber.key)
        haydn.level = "top"
        haydn.title = "Haydn Trumpet Concerto"
        haydn.put()

It seems to me as though I'm trying to do basically the same thing.. but I get this error:

File "... Application\datastoreexplore\main.py", line 41, in get haydn = Music(parent=subscriber.key) BadValueError: Expected string, got Key('Subscriber', '12169615523634875051')

I've spent hours reading docs and searching through stackoverflow and elsewhere. I'm stumped. What am I doing wrong??

هل كانت مفيدة؟

المحلول

Your problem is thou have defined parent as a StringProperty but then passing in a key which is a valid constructor argument if you want to create an entity with anancestor. You should not have property called parent.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top