Filter query by key name

178 views
Skip to first unread message

S.Prymak

unread,
Aug 29, 2011, 3:33:29 PM8/29/11
to google-a...@googlegroups.com
I have to query datastore for entities with key names which are greater or equals to specified string. Something like this:

def get_pages_by_key_name(key):
  p = models.Page(key_name=key)
  query = GqlQuery("SELECT * FROM Page WHERE 
???? :1", p.key.name())
  return [i for i in query]

How can I do that?

Joseph Letness

unread,
Aug 30, 2011, 12:41:31 PM8/30/11
to Google App Engine
Hi, the model class already has a built in function for retrieving an
entity by key name:

my_entity = MyModel.get_by_key_name(key_name)

Also, key names are unique within a data model. Your example shows an
iteration over multiple entities which will never happen if you are
querying on a key name.

Hope this helps.

--Joe

S.Prymak

unread,
Aug 30, 2011, 4:20:04 PM8/30/11
to google-a...@googlegroups.com
Thank you for reply, Joseph.

I know about get_by_key_name. Now I see that my example is not correct.

I need all entites wich key().name() is greater or equal to specified string. May be my example query should be GqlQuery("SELECT * FROM Page WHERE __key__.name >= :1"p.key.name()) which is not a correct query in current implementation of GAE. Comparing keys like "__key__ >= :1" does not work for me.

S.Prymak

unread,
Aug 30, 2011, 4:39:38 PM8/30/11
to google-a...@googlegroups.com
Here is why comparing key does not work:

import models
pk=models.Page(key_name='_blocks')
q=models.Page.gql("WHERE __key__ >= :1", pk.key())
[p.key().name() for p in q]
[u'_blocks--callout', u'_blocks--callout-contact', u'_blocks--footer-center', u'_blocks--teaser', u'_slides--home--1', u'about', u'collections', u'default', u'faq', u'shipping']


In my implementation key name is a "slug" for page. I want only those pages wich slug begins with '_blocks'. Is it possible quering by key name?

I thought about [p for p in models.Page.all() if p.key().name() >= '_blocks'], but I doubt it has good perfomance on large set of entities.

Mark Bucciarelli

unread,
Aug 30, 2011, 4:51:17 PM8/30/11
to google-a...@googlegroups.com
i don't think you can do what you want.

in gql condition, you only have access to
__key__, which i bet is some hash of
kind + keyname + ...

so, keys_only query, then scan results.

or add another string prop and then
use some trick like 

  q.filter('key_s >=', u'_block\u0000')
  q.filter('key_s <=', u'_block\u9999)

m

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/Z8h6AQSoQw4J.

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.

Stephen Johnson

unread,
Aug 30, 2011, 5:51:28 PM8/30/11
to google-a...@googlegroups.com
You definitely can do queries by your keys. I'm not a Python person but it looks to me like your problem is that you've forgotten the <= part of the query so you get the _slides objects because _slides is > _blocks. You also get faq, default, shipping etc. because the underscore character is less than in Unicode (and thus ASCII) than the lower case letters. So try adding: __key++ <= :2 where :2 is a key equal to '_blocks' + \uFFFD'

Stephen

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.

Stephen Johnson

unread,
Aug 30, 2011, 5:52:01 PM8/30/11
to google-a...@googlegroups.com
I meant __key__ not __key.

Stephen Johnson

unread,
Aug 30, 2011, 5:52:23 PM8/30/11
to google-a...@googlegroups.com
I meant _key__ not __key++. Damn fingers.

S.Prymak

unread,
Aug 30, 2011, 6:20:44 PM8/30/11
to google-a...@googlegroups.com
Thank you, Stephen! You are totally right!

That's solved my problem!  That was my stupid, stupid assumption about string comparision algoritm :(

Stephen Johnson

unread,
Aug 30, 2011, 8:10:23 PM8/30/11
to google-a...@googlegroups.com
Your welcome. Glad to be of help!

On Tue, Aug 30, 2011 at 3:20 PM, S.Prymak <spr...@gmail.com> wrote:
Thank you, Stephen! You are totally right!

That's solved my problem!  That was my stupid, stupid assumption about string comparision algoritm :(

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/EdaT7weIv_8J.
Reply all
Reply to author
Forward
0 new messages