Why can't I filter() based on key?

38 views
Skip to first unread message

Mike

unread,
Mar 20, 2012, 5:26:20 PM3/20/12
to Google App Engine
I am trying to use the filter() method based on a key argument. For
example:

user_key="agxkZXZ-emFiZXRhLTJyCgsSBFVzZXIYGAw"
d=datamodel.Task.all()
d.filter("user=",user_key)
results=d.fetch(1024)
This produces zero results, and its probably because the key isn't in
the right format. If this where gql would I have to use the Key()
function like this:

db.gqlQuery("select * from Task where user=Key(:1)",user_key)
However I don't think I can use the Key() function within a filter,
but there must be some python functional equivalent... Is there a way
to properly format a key to be used within the filter() method?

User is defined as:
user = db.ReferenceProperty(None)

Andreas

unread,
Mar 21, 2012, 8:57:15 AM3/21/12
to google-a...@googlegroups.com
dont know if this is a typo but there is a strict syntax on how to query.

you are missing some spaces in your filter():

d.filter('user =', user_key)

there is a space between property name ane =

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>

Kenneth

unread,
Mar 21, 2012, 9:42:28 AM3/21/12
to google-a...@googlegroups.com
I remember get tripped up on that space, boy was that painful. You can also leave out the = entirely, it is assumed if no operator is given.

Using filter you can use a key object directly, I have no experience with using a string with filter.


On Wednesday, March 21, 2012 12:57:15 PM UTC, aschmid wrote:
dont know if this is a typo but there is a strict syntax on how to query.

you are missing some spaces in your filter():

d.filter('user =', user_key)

there is a space between property name ane =

On Mar 20, 2012, at 5:26 PM, Mike wrote:

> I am trying to use the filter() method based on a key argument. For
> example:
>
> user_key="agxkZXZ-emFiZXRhLTJyCgsSBFVzZXIYGAw"
> d=datamodel.Task.all()
> d.filter("user=",user_key)
> results=d.fetch(1024)
> This produces zero results, and its probably because the key isn't in
> the right format. If this where gql would I have to use the Key()
> function like this:
>
> db.gqlQuery("select * from Task where user=Key(:1)",user_key)
> However I don't think I can use the Key() function within a filter,
> but there must be some python functional equivalent... Is there a way
> to properly format a key to be used within the filter() method?
>
> User is defined as:
> user = db.ReferenceProperty(None)
>
> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.

> To post to this group, send email to google-appengine@googlegroups.com.
> To unsubscribe from this group, send email to google-appengine+unsubscribe@googlegroups.com.

Guido van Rossum

unread,
Mar 21, 2012, 3:29:36 PM3/21/12
to google-a...@googlegroups.com
You have to wrap user_key in a call to db.Key(), e.g.

d.filter("user =", db.Key(user_key) )

The user_key value you have is a string but the datastore contains a Key object, which is a structured thing that will never match any string.

--Guido van Rossum
Reply all
Reply to author
Forward
0 new messages