Python db.filter() strangeness

30 views
Skip to first unread message

sergey

unread,
Jun 23, 2011, 4:50:58 AM6/23/11
to Google App Engine
Hi all!
I have spend more my time to find bug in my code, source of which is a
feature of the filtering methods:
Сompare the two codes and try to quickly find a mistake in one of them
(which you do not show any IDE):

code 1:

from google.appengine.ext import db
class Item(db.Model):
name = db.StringProperty()

Item(name= 'ipad').put()
ipads = Item.all().filter('name =', 'ipad').count()

print ipads

code 2:

from google.appengine.ext import db
class Item(db.Model):
name = db.StringProperty()

Item(name= 'ipad').put()
ipads = Item.all().filter('name=', 'ipad').count()

print ipads


what the code is correct?

i think you need to to fix this feature in future releases

p.s. filter('name=', 'ipad') != filter('name =', 'ipad')

Robert Kluin

unread,
Jun 26, 2011, 1:44:33 AM6/26/11
to google-a...@googlegroups.com
Hi Sergey,
This is a known and expected behavior, it is mentioned in the docs:
http://code.google.com/appengine/docs/python/datastore/queryclass.html#Query_filter

Datastore property names don't have to be valid Python identifiers.
When there is no space in the filter def, equality is assumed. I do
agree that this is an easy mistake to make. But, I know I've got lots
of filters in my code that omit the operator for brevity. And who
knows, someone probably also has an "=" in a property name.


Robert

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

Stephen

unread,
Jun 27, 2011, 6:52:24 AM6/27/11
to google-a...@googlegroups.com
2011/6/26 Robert Kluin <robert...@gmail.com>:

>
> And who knows, someone probably also has an "=" in a property name.


Google knows. They could map over all properties in the datastore
checking for names which end in '='. I would be surprised if it's not
0.

Robert Kluin

unread,
Jun 27, 2011, 11:34:50 AM6/27/11
to google-a...@googlegroups.com

You have more faith than I do. :)

Jay Young

unread,
Jun 27, 2011, 11:43:44 AM6/27/11
to google-a...@googlegroups.com
If anyone is base-64 encoding their property names, this would be highly likely.  Or, if anyone is mapping readable property names to a shorter alphabet to save on storage size, a property named "=" would also be a possibility.

Branko Vukelic

unread,
Jun 27, 2011, 1:28:11 PM6/27/11
to google-a...@googlegroups.com
On Thu, Jun 23, 2011 at 10:50 AM, sergey <s.lya...@gmail.com> wrote:
> ipads = Item.all().filter('name =', 'ipad').count()

Sometimes happens to me in the form of typo. I've adopted this form:

ipads = Item.all().filter('name', 'ipad').count()

It's documented.


--
Branko Vukelić
bra...@herdhound.com

Lead Developer
Herd Hound (tm) - Travel that doesn't bite
www.herdhound.com

Reply all
Reply to author
Forward
0 new messages