这是我的代码。我跳过了一些部分。
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 类中时,它就可以工作了。