Not Equal Operator in Filter with datastore.Query in GAE with Python

982 views
Skip to first unread message

Mitul Golakiya

unread,
Oct 9, 2013, 9:32:03 AM10/9/13
to google-a...@googlegroups.com
Hello All,

We are developing one app on GAE with python.

We are using datastore.py for querying data from datastore, because we have to define our entities at runtime. So we can not use db.Model to define models. And retrieve data by models.

We have to use "Not Equal" operator to retrieve some data from datastore.
Suppose, I want to retrieve all Persons, with name is not equal to "Test".

Here is my code :

filerObj = {}
filterObj["name!="] = "Test"

dbObj = datastore.Query("Person", filterObj)
dbObj.Run()
recordsList = dbObj.Get()


By this code, we are getting empty result.
Any idea about what's wrong ??

Thanks in Advance...

Vinny P

unread,
Oct 9, 2013, 8:36:23 PM10/9/13
to google-a...@googlegroups.com
On Wed, Oct 9, 2013 at 8:32 AM, Mitul Golakiya <mtl.go...@gmail.com> wrote:
We are using datastore.py for querying data from datastore, because we have to define our entities at runtime. So we can not use db.Model to define models. And retrieve data by models.

We have to use "Not Equal" operator to retrieve some data from datastore.
Suppose, I want to retrieve all Persons, with name is not equal to "Test".

By this code, we are getting empty result.
Any idea about what's wrong ??



Does your code return any entities if filterObj is empty? Are you running on the dev datastore or production?

 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Alfred Fuller

unread,
Oct 10, 2013, 12:21:46 AM10/10/13
to Google App Engine
FYI both ndb and db support dynamic properties through the Expando class


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.

Mitul Golakiya

unread,
Oct 10, 2013, 1:50:03 AM10/10/13
to google-a...@googlegroups.com
Thanks for the reply...

I am getting all records while filterObj is empty & Currently I am using localhost datastore with GAE SDK 1.8.5...

Mitul Golakiya

unread,
Oct 10, 2013, 2:00:57 AM10/10/13
to google-a...@googlegroups.com
Thanks Alfred...

To use both ndb or Expando class, first I have to create a class which extends ndb or Expando...
But I am getting entity name at run time, So I do not have any fixed entity.

Our users can store their own entities at run time.
Suppose I am creating one class with "CustomEntity" which extends expando, and I am giving custom entity name in  "Key Name", then all the entities will be stored in table "CustomEntity".

But is there any way store each entity in separate tables with table name as entity name ??

timh

unread,
Oct 10, 2013, 7:39:00 AM10/10/13
to google-a...@googlegroups.com
First off you should have spaces between the name and the operator in the query string.

However that won't solve you problem in this case in the code of datastore.Query you will see the following docs

> query = Query('Person')
  > query.update({'name =': 'Ryan', 'age >=': 21})

  The supported operators are =, >, <, >=, and <=. Only one inequality
  filter may be used per query. Any number of equals filters may be used in
  a single Query.

With a bit of further digging in appengine.datastore.datastore_query you can see the set of _INEQUALITY_OPERATORS  and != is not one of them

So you will need to rethink what you are doing.

T

timh

unread,
Oct 10, 2013, 7:39:16 AM10/10/13
to google-a...@googlegroups.com
As an aside you really misunderstand how  the datastore works there are no tables.

Mitul Golakiya

unread,
Oct 10, 2013, 11:33:04 PM10/10/13
to google-a...@googlegroups.com
Sorry about writing mistake,

I was talking about Entity. All records are saved in one common entity CustomEntity.
I want to define entities at run-time.

Any idea about that ??

Alfred Fuller

unread,
Oct 11, 2013, 12:35:51 AM10/11/13
to Google App Engine
Ideally you should be able to tell ndb to use an expando class as default for any kind you don't have a concrete model class for. It looks like doing that is not supported (and slightly broken). Can you file a feature request?

timh

unread,
Oct 11, 2013, 1:54:49 AM10/11/13
to google-a...@googlegroups.com
You could use metaclasses to create dynamic models, however you would always have to create a matching class (which would register the class to kind map)
before you could perform any query.

I think you should elaborate more on what you are trying to do, we may be able to suggest an approach which doesn't require you to go this far.

Regards

Tim

Alfred Fuller

unread,
Oct 11, 2013, 2:09:43 AM10/11/13
to Google App Engine
FYI, the following function should be sufficient to register a dynamic kind in the kind_map:

def addExpandoForKind(kind):
  class Dummy(db.Expando):
    @classmethod
    def _get_kind(cls):
      return kind

though it is not a "good" solution :-) (there are all sorts of memory, thread and request related issues)



--

Mitul Golakiya

unread,
Oct 11, 2013, 3:44:56 AM10/11/13
to google-a...@googlegroups.com
I have tried this code, but still it is creating entity as class name (DynamicEntity) not as given my custom entity name(MyCustomEntity):

        class DynamicEntity(db.Expando):
            @classmethod
            def _get_kind(cls):
                return 'MyCustomEntity'

        newEntity = DynamicEntity()
        newEntity.fname = "Test"
        newEntity.put()

Mitul Golakiya

unread,
Oct 11, 2013, 3:50:54 AM10/11/13
to google-a...@googlegroups.com
Thanks Tim Hoffman,

We are creating one app in which, on admin side User can create his own entities and can create fields in that entity.
So as the documentation I have read, to store any entity in datastore which have dynamic fields, we have to use ndb or Expando.

But I am getting entity name at runtime, so I am not able to define static classes for my entities.
So how can I create dynamic entities with dynamic field support ??

Alfred Fuller

unread,
Oct 11, 2013, 3:52:22 AM10/11/13
to Google App Engine
Thats because you are using 'db' instead of 'ndb'

timh

unread,
Oct 11, 2013, 5:43:43 AM10/11/13
to google-a...@googlegroups.com
As I said

I would use a metaclass to create classes on demand.
However you would need to use some name mangling scheme so that each users class name was garunteed to be unique otherwise 
you could end up in all sorts of mess.

Alternately specify an entity name as a property of the class (or use it to define the namespace), and use the same class for all users, along with their own custom properties.

this would be a much simpler approach.

Tim

timh

unread,
Oct 11, 2013, 5:57:32 AM10/11/13
to google-a...@googlegroups.com
Ignore the namespace comment.

Mitul Golakiya

unread,
Oct 11, 2013, 6:26:07 AM10/11/13
to google-a...@googlegroups.com
Thanks Alftred, It worked...

Thank you so much for you help...

Alfred Fuller

unread,
Oct 14, 2013, 10:05:27 PM10/14/13
to Google App Engine
FYI, I do not recommend you use that solution as it registers kind in the kind_map for all requests used by a given instance. Instead there should be an option to decode undeclared kinds using an Expando. This feature actually does exist, though I noticed it was broken when using memcache. I recommend you file a feature request to support this use case so we can track a good solution.

Mitul Golakiya

unread,
Oct 14, 2013, 11:44:48 PM10/14/13
to google-a...@googlegroups.com
I will submit a feature request for this....
Reply all
Reply to author
Forward
0 new messages