querying list of nested objects

9 views
Skip to first unread message

Jacob G

unread,
Sep 23, 2019, 10:03:58 PM9/23/19
to djangae-users
Using ndb, I can query for entities from a list of nested objects like this:


from djangae.test import TestCase
from google.appengine.ext import ndb


class Label(ndb.Model):
id = ndb.StringProperty()
name = ndb.StringProperty()


class ModelWithLabels(ndb.Model):
labels = ndb.StructuredProperty(Label, repeated=True)


class NdbComplexFieldsTestCase(TestCase):

def test_query_from_list_of_objects(self):

entity = ModelWithLabels(
labels=[
Label(
id='custom:123',
name='strong'
),
Label(
id='custom:234',
name='fast'
)
]
)
entity.put()

query = ModelWithLabels.query(ModelWithLabels.labels.id == 'custom:123')

self.assertEqual([i.key for i in query.fetch(1)], [entity.key])


But it doesn't seem like that might be possible with djangae, as shown in the following failing test:


@override_settings(GENERATE_SPECIAL_INDEXES_DURING_TESTING=True)
def test_can_query_from_list_of_values(self):

e = JsonFieldModel.objects.create(labels=[
{
'id': 'department:123',
'name': 'Office'
}
])

query = JsonFieldModel.objects.filter(labels__id='department:123')

self.assertEqual(list(i.id for i in query), [e.id])


The documentation seems to say that:

Are there are any workarounds? I think there may be clever ways to accomplish the same idea, but perhaps it won't be schema-compatible with a side-by-side ndb implementation.
Reply all
Reply to author
Forward
0 new messages