khany
khany

Reputation: 1187

how to use google user id in entities - python appengine

I create an entity like so:

company = dbDatafile(usOwner = user)
company.name = self.request.get("name")
company.put()

where user is the Google User account. Now when I try to search on that user like:

datafiles = dbDatafile.gql("WHERE usOwner = '%s'" % user.user_id())

assign to a jinja2 template var:

template_values = {
    'datafiles': datafiles
}

and output to html:

{% for datafile in datafiles %}
    >>>{{ datafile.name }}         chevrons to indicate any looping
{% endfor %}

i get no output.

The data is there in the admin dashboard but I can't access it. Any ideas?

Thanks

Upvotes: 2

Views: 157

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

If usOwner is a UserProperty, you need to use USER() in GQL:

datafiles = dbDatafile.gql("WHERE usOwner = USER(:1)", user.email())

Also remember always to use placeholders, not string interpolation - just because it's not SQL, doesn't mean it's not vulnerable to injection.

Upvotes: 5

khany
khany

Reputation: 1187

OK this isn't strictly answering my question but a slight workaround. I decided to use the parent feature so my code now looks like this:

company = dbDatafile(parent=db.Key.from_path('user', user.user_id()))
company.name = self.request.get("name")
company.usOwner = user
company.put()


datafiles = dbDatafile.gql("WHERE ancestor IS '%s'" % db.Key.from_path('user', user.user_id()))

Not sure if this is ideal but it works and is compatible with the High Replication datastore.

Upvotes: 0

Related Questions