0

这是我的代码。我跳过了一些部分。

class Members(db.Model):
    author = db.UserProperty()
    username = db.StringProperty()

class Events(db.Model):
    author = db.ReferenceProperty(Members)

class MainPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            member = Members()
            member.author = user
            member.username = ''
            member.put()

class AddEvent(webapp.RequestHandler):
    def post(self):
        event = Events()
        event.author = Members(author=users.get_current_user())
        event.put()
        self.redirect('/add')
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'templates', 'add_event.html')
        self.response.out.write(template.render(path, None))

添加新事件时,我收到此错误BadValueError: Members instance must have a complete key before it can be stored as a reference。我做了一些搜索,当我们不保存实体实例(put())时发生了这个错误。所以我添加了将用户保存到数据存储区的代码,但我得到了同样的错误。当我将代码保存在 AddEvent 类中时,它就可以工作了。

4

1 回答 1

1

我认为您想将您的 post() 更改为这样的内容(未经测试):

def post(self):
    member = Members.all().filter("author =", users.get_current_user()).get()
    event = Events()
    event.author = member
    event.put()
    self.redirect('/add')

您不想创建新的 Members 实体,而是想检索您已经创建的实体。

于 2012-06-01T01:18:59.507 回答