This repository was archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
This repository was archived by the owner on Dec 25, 2023. It is now read-only.
Filtering nested StructuredProperty doesn't work #286
Copy link
Copy link
Open
Description
According to the documentation here, I should be able to match multiple properties on a (repeated) StructuredProperty in my query. If, however, I nest the property inside another StructuredProperty (not repeatedly) the query fails.
NB: Ignore the dummy parent, I put it there so we can run this code consistently
from google.appengine.ext import ndb
from pprint import pprint
class DummyParent(ndb.Model):
pass
class Address(ndb.Model):
city = ndb.StringProperty()
state = ndb.StringProperty()
class Place(ndb.Model):
address = ndb.StructuredProperty(Address, required=True)
tag = ndb.StringProperty()
class Contact(ndb.Model):
fullname = ndb.StringProperty()
places = ndb.StructuredProperty(Place, repeated=True)
dummy_key = ndb.Key(DummyParent, '123')
c1 = Contact(parent=dummy_key,
fullname='American Man',
places=[
Place(address=Address(city='Vancouver', state='Washington'), tag='Hometown'),
Place(address=Address(city='Mountain View', state='California'), tag='Work')
])
c1.put()
c2 = Contact(parent=dummy_key,
fullname='Canadian Expat',
places=[
Place(address=Address(city='Vancouver', state='British Columbia'), tag='Hometown'),
Place(address=Address(city='Seattle', state='Washington'), tag='Work')
])
c2.put()
print "BEWARE! This wont work!"
for c in Contact.query(Contact.places.address.city == "Vancouver",
Contact.places.address.state == "Washington",
ancestor=dummy_key):
pprint(c.to_dict())
print "Query using Address, Still returns Canadian Expat:"
for c in Contact.query(Contact.places.address == Address(city="Vancouver",
state="Washington"),
ancestor=dummy_key):
pprint(c.to_dict())
print "Query using Place, Still returns Canadian Expat!:"
for c in Contact.query(Contact.places == Place(address=Address(
city="Vancouver",
state="Washington")),
ancestor=dummy_key):
pprint(c.to_dict())
Here's the output:
BEWARE!
{'fullname': u'Canadian Expat',
'places': [{'address': {'city': u'Vancouver', 'state': u'British Columbia'},
'tag': u'Hometown'},
{'address': {'city': u'Seattle', 'state': u'Washington'},
'tag': u'Work'}]}
{'fullname': u'American Man',
'places': [{'address': {'city': u'Vancouver', 'state': u'Washington'},
'tag': u'Hometown'},
{'address': {'city': u'Mountain View', 'state': u'California'},
'tag': u'Work'}]}
COMBINED! STILL NOT WORKING!
{'fullname': u'Canadian Expat',
'places': [{'address': {'city': u'Vancouver', 'state': u'British Columbia'},
'tag': u'Hometown'},
{'address': {'city': u'Seattle', 'state': u'Washington'},
'tag': u'Work'}]}
{'fullname': u'American Man',
'places': [{'address': {'city': u'Vancouver', 'state': u'Washington'},
'tag': u'Hometown'},
{'address': {'city': u'Mountain View', 'state': u'California'},
'tag': u'Work'}]}
COMBINED SOME MORE! STILL NOT WORKING!
{'fullname': u'Canadian Expat',
'places': [{'address': {'city': u'Vancouver', 'state': u'British Columbia'},
'tag': u'Hometown'},
{'address': {'city': u'Seattle', 'state': u'Washington'},
'tag': u'Work'}]}
{'fullname': u'American Man',
'places': [{'address': {'city': u'Vancouver', 'state': u'Washington'},
'tag': u'Hometown'},
{'address': {'city': u'Mountain View', 'state': u'California'},
'tag': u'Work'}]}
Metadata
Metadata
Assignees
Labels
No labels