Unable to get results when passing a string via parameter substitution in gql query
-
11-09-2019 - |
Question
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 2
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.
OTHER TIPS
Have you tried filter('user_name = ', str(user_name))
?
I supose you are sure user_name has the expected content.
Try logging repr(user_name)
to verify that the string is exactly the same as what you're expecting (and that it's not unicode rather than raw). Also try logging the expression user_name == "peter rice"
. Other than that, I can't see any reason why it would not work - there's literally no way the API can affect this, since it doesn't know where the argument you pass in comes from.