Search code examples
pythongoogle-app-enginegqlgqlquerystring-substitution

Unable to get results when passing a string via parameter substitution in gql query


I am able to properly pass a string variable to the gqlquery through parameter substitution, here's the code i've tried to use;

user_name = self.request.get('username') #retrieved from UI
p = models.UserDetails.all().filter('user_name = ', user_name).fetch(1)

I don't get any results and the query fails silently. But when I hard code the query like this ,

p = models.UserDetails.all().filter('user_name = ', "peter rice").fetch(1)

I get my expected resultset. I think I am passing the variable user_name in a wrong way, Please help me in getting my piece of code right.


Solution

  • I think I've got it, I tried using this,

    p = models.UserDetails.gql('WHERE user_name = :uname', uname = user_name).fetch(1)
    

    and I got the expected resultset. I wonder why other formats have the problem in string substitution.