1
votes

Is there any way of using JsonProperties in queries in NDB/GAE? I can't seem to find any information about this.

Person.query(Person.custom.eye_color == "blue").fetch()

With a model looking something like this:

class Person(ndb.Model):
    height = ndb.IntegerProperty(default=-1)
    #...
    #...
    custom = ndb.JsonProperty(indexed=False, compressed=False)

The use case is this: I'm storing data about customers, where we at first only needed to query specific data. Now, we want to be able to query for any type of registred data about the persons. For example eye color, which some may have put into the system, or any other custom key/value pair in our JsonProperty.

I know about the expando class but for me, it seems a lot easier to be able to query jsonproperty and to keep all the custom properties on the same "name"; custom. That means that the front end can just loop over the properties in custom. If an expando class would be used, it would be harder to differentiate.

1

1 Answers

2
votes

Rather than using a JSONProperty have you considered using a StructuredProperty. You maintain the same structure, just stored differently and you can filter by sub components of the StructureProperty with some restrictions, but that may be sufficient.

See https://developers.google.com/appengine/docs/python/ndb/queries#filtering_structured_properties

for querying StructuredProperties.