views:

99

answers:

3

Hey I have this code but it doesn't work because it is expecting a string. How can I make it work?

class Atable(BaseModel):
    owner = db.UserProperty()
        (...)

--------- // --------------
query = "SELECT * FROM Atable WHERE owner=", users.get_current_user()
results = db.GqlQuery(query)

How can I fix that search? Thanks :)

I've started with the appengine database yesterday so be gentle :)

+2  A: 
query = GqlQuery("SELECT * FROM Atable WHERE owner = :1", users.get_current_user())
Adam Crossland
No, just "WHERE owner = :1" - adding USER will make it expect a string.
Nick Johnson
+2  A: 

You could try the GQL way:

results = db.GqlQuery("SELECT * FROM Atable WHERE owner = :1", users.get_current_user().key())

or the Python Query way:

query = db.Query(Atable)
results = query.filter('owner =', users.get_current_user())
Thierry Lam
A: 

The python query way is probably easier if you just need to get some data for that user.

For example in your class Something(db.Model), you can define:

@staticmethod
def get_something_by_user(user):

        query = db.Query(Something).filter('users =', user)
        result = query.fetch(limit=1000)

        return result

Then you call it by doing this:

user = users.GetCurrentUser()
results = Something.get_something_by_user(user)
Sologoub