I think I found a (partial) solution to my problem, after reading this SO answer.
I can simply call objects.get()
recursively starting from the top-level document.
For example, considering my schema, I can query a given label (embedded-document) with:
label = Image.objects.get(id=image_id).labels.get(id=label_id)
And I can query a given feature (nested embedded-document) with:
feature = Image.objects.get(id=image_id).labels.get(id=label_id).features.get(id=feature_id)
But these queries require the parent documents IDs.
I can obtain a parent image ID based on a given label ID with objects.filter()
.
parent_image = Image.objects.filter(labels__id=label_id)[0]
I can also obtain a parent image ID based on a given feature ID:
parent_image = Image.objects.filter(labels__features__id=feature_id)[0]
But I can’t seem to obtain the parent label ID based on the feature ID:
parent_label = Image.objects.get(id=parent_image.id).labels.filter(features__id=feat_id)[0]
# Raises "AttributeError: 'Label' object has no attribute 'features__id'"