On Sun, Sep 13, 2009 at 10:32 PM, nikihana <[email protected]> wrote: > > Hi All, > > I am new to python - so this might be embarrassing - but thanks in > advance! > > Can somebody tell me why I get an error when I uncomment the line in > the code below and run it in the appengine interactive shell? > > The error I get is: > > 'title =', 'song' > Traceback (most recent call last): > File "/Users/srini/Desktop/iFaceApps/SVN/unfuddle/iChannel/App > Engine/App Engine/GoogleAppEngineLauncher.app/Contents/Resources/ > GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/ > google/appengine/ext/admin/__init__.py", line 208, in post > exec(compiled_code, globals()) > File "<string>", line 22, in <module> > TypeError: filter() takes exactly 3 arguments (2 given) > > > ------code begins-------- > > class Song(db.Model): > title = db.StringProperty() > composer = db.StringProperty() > > song = Song() > > song.title = "song" > song.composer = "me" > song.put() > > query = db.Query(Song) > > attr = 'title' > value = 'song' > > query_string = '\'' + attr + ' =' + '\', ' + '\'' + value + '\'' > > print query_string > query = Song.all() > > query.filter('title =', "song") # no error! > #query.filter(query_string) #error! > result = query.fetch(1000) > > print result[0]
Because filter takes 2 arguments, not 1 (there is also the implicit 'this/self', which is why the error message says 3/2). If you want to pass a query string in to filter, then make it a tuple and deconstruct it into the call, as such: query_string = (attr + ' =', value) And call filter by deconstructing the tuple as the arguments: query.filter(*query_string) #no more error! The function call parameters is standard programming stuff that is identical to just about any language, and the deconstructing the tuple as the argument is very common python stuff. You might want to learn some more generic programming stuff first. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---
