I have this two models: Tutorial and Chapter. Chapters have a key reference to the tutorial they belong. Now I’m trying to list all the chapter that belong to a specific Tutorial but I can’t get the query to work:
class TutView(FuHandler):
def get(self):
tutKey = self.request.get('tut_key')
tut = db.Key.from_path('Tutorial', tutKey)
chaps = db.GqlQuery("SELECT * FROM Chapter " +
"WHERE __key__ = KEY('Tutorial', :1)", tut)
self.render('tutView.html', chaps=chaps)
When running I get this error:
BadArgumentError: Expected an integer id or string name as argument 2;
received datastore_types.Key.from_path(u'Tutorial', u'<bound method
Tutorial.key of <main.Tutorial object at 0x00.............
Chapter model:
class Chapter(db.Model):
tutorial = db.ReferenceProperty(Tutorial, collection_name='chapters')
title = db.StringProperty(required=True)
content = db.TextProperty(required=True)
Tutorial model:
class Tutorial(db.Model):
title = db.StringProperty(required=True)
presentation = db.TextProperty(required=True)
update:
i managed to get no errors but i get no results (so, probably it’s wrong):
chaps = db.GqlQuery("SELECT * FROM Chapter " +
"WHERE __key__ = KEY('tutorial', :1)", str(tutKey))
update2:
The problem might be on the way i store or retrieve the tutKey. I’m getting the key with the URL… and and that’s probably not working… but i don’t know other way
The
KEY()GQL function is used to create Keys based on two parameters: The Entity, and the name/id.If you look at your code, you will see that you are passing it a
db.Keyobject as a parameter.Hence, the error message:
Also, you need to test against the
tutorialproperty of theChapterentity, and not against its own__key__.I do not work with GQL, I use the Query objects instead. You provide them with the
db.Keyobject directly, so I assume this should work:If it doesn’t, you can always try: