Filter Key

66 views
Skip to first unread message

Max

unread,
Oct 22, 2011, 4:30:11 PM10/22/11
to google-a...@googlegroups.com
Hi,
I'm trying to filter by Key, but it's not working. I have searched in the documents, but there is nothing useful (or understandable for me).

My code is:
class ModificaHandler(webapp.RequestHandler):
def get(self, domain):
appo = self.request.get('domain')
risto = db.Query(RISTORANTI).filter("key =", appo)
self.response.out.write(template.render('risto.html',{'risto':risto}))

I can't understand why. Please help.

Max

Jeff Schnitzer

unread,
Oct 22, 2011, 5:31:17 PM10/22/11
to google-a...@googlegroups.com
The key field is "__key__" (two underscores on each side).

Jeff

--
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/-/gMMnMu2YMj8J.
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.

Max

unread,
Oct 22, 2011, 5:43:15 PM10/22/11
to google-a...@googlegroups.com
Thanks, but now I have something new.

BadFilterError: BadFilterError: invalid filter: __key__ filter value must be a Key; received  (a str).

I'm taking the key from the domain name:
def main():
application = webapp.WSGIApplication([
('/ristorante', RistoHandler),
(r'/(.*)/modifica', ModificaHandler),
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
Can you help me with this too?
Thanks
Max


Amy Unruh

unread,
Oct 22, 2011, 5:51:27 PM10/22/11
to google-a...@googlegroups.com
Max,

It looks as though you may be intending to fetch an object based on its key name rather than its key?  If you build an object using a specified key name, e.g.:
  MyModel.get_or_insert(key_name='some_key')

Then you can fetch it like this:

  myobject = MyModel.get_by_key_name('some_key')

or  alternately:

  k = db.Key.from_path('MyModel', 'some_key')
  myobject = db.get(k)
  
 –Amy

--
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/-/LO6RGlxuTPMJ.

Max

unread,
Oct 22, 2011, 5:59:34 PM10/22/11
to google-a...@googlegroups.com
I've tried your method but I'm still having an error.

In my template I'm bulding a link by the key:
<a href=\"/{{ elemento.key}}/modifica\">  Modifica/Commenti </a>

then:
def main():
application = webapp.WSGIApplication([
('/ristorante', RistoHandler),
(r'/(.*)/modifica', ModificaHandler),
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

then:

class ModificaHandler(webapp.RequestHandler):
def get(self, domain):
appo = self.request.get('domain')
risto = RISTORANTI.get_by_key_name(appo)
self.response.out.write(template.render('risto.html',{ 'risto':risto}))


The error is:
Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 634, in __call__
    handler.get(*groups)
  File "C:\Users\Massimiliano\Desktop\AppEngine Application\Thepietronis\main.py", line 39, in get
    risto = RISTORANTI.get_by_key_name(appo)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1188, in get_by_key_name
    for name in key_names]
  File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_types.py", line 489, in from_path
    ValidateString(id_or_name, 'name')
  File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_types.py", line 158, in ValidateString
    raise exception('%s must not be empty.' % name)
BadValueError: name must not be empty.

Max

unread,
Oct 22, 2011, 6:01:43 PM10/22/11
to google-a...@googlegroups.com
I'm not building the key, but I'm using the given key of the object.

Max

unread,
Oct 23, 2011, 7:25:03 AM10/23/11
to google-a...@googlegroups.com
Do someone know how to sort it out?

Amy Unruh

unread,
Oct 23, 2011, 4:56:52 PM10/23/11
to google-a...@googlegroups.com
Max,

It looks like you need to be working with a string-encoded key when you generate the client-side pages, which you can then convert back to a Key server-side as needed.  You can obtain the string-encoded version by passing the Key object to str(). See http://code.google.com/appengine/docs/python/datastore/keyclass.html for details.

 -Amy

On Sun, Oct 23, 2011 at 10:25 PM, Max <massimilia...@gmail.com> wrote:
Do someone know how to sort it out?
--
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/-/RTdz9gXXFlYJ.
Reply all
Reply to author
Forward
0 new messages