Google App Engine +关系数据库 - 获取ID

时间:2010-12-29 17:44:21

标签: google-app-engine google-cloud-datastore relational-database

我有一个问题,我的db模型如下:

class UserAnswer (db.Model):
user = db.UserProperty()
name = db.StringProperty()
email = db.StringProperty()
occupation = db.IntegerProperty()
howreach = db.IntegerProperty()
rating = db.IntegerProperty()
comment = db.StringProperty(multiline=True)

class Involves(db.Model):
shortName = db.StringProperty()
fullName = db.StringProperty()

class UserInvolves(db.Model):
userAnswer = db.ReferenceProperty(UserAnswer)
involve = db.ReferenceProperty(Involves)

如何选择以下内容:

def getInvolves(user):
results = db.GqlQuery("Select * from UserInvolves as ui where ui.userAnswer.ID='"+user.ID+"'")

我的意思是我怎么能得到这个id键?正如我上面尝试的那样,我发现了一个错误:

Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 517, in __call__
handler.post(*groups)
File "C:\Program Files\Google\google_appengine\demos\b00213576\main.py", line 217, in post
involves = getInvolves(answer)
File "C:\Program Files\Google\google_appengine\demos\b00213576\main.py", line 128, in getInvolves
results = db.GqlQuery("Select * from UserInvolves as ui where ui.userAnswer.ID='"+user.ID+"'")
AttributeError: 'UserAnswer' object has no attribute 'ID'

我不知道如何转换为GAE:/

1 个答案:

答案 0 :(得分:0)

user_answer = UserAnswer.get_by_id(user.ID)
for user_involve in user_answer.userinvolves_set:
    # these are your UserInvolves items