class Parent(document):
name = StringField()
children = ListField(ReferenceField('Child'))
class Child(document):
name = StringField()
parents = ListField(ReferenceField(Parent))
@app.route('/home/')
def home():
parents = Parent.objects.all()
return render_template('home.html', items=parents)
I have two collections similar to that above, which maintain a many to many relationship.
In the template with Angular, I'm setting a javascript variable to a list of Parents like so:
$scope.items = {{ parents|tojson }};
This results in an array of Parents who'se chilren
are an array of Object Ids (references), as opposed to dereferenced child
objects:
$scope.items = [{'$oid': '123', 'name': 'foo', 'children': [{'$oid': '456'}]}];
I want this angular object to contain all of the dereferenced children. Is there an efficient way to do this?
So far, this is the only approach that to works for me, at O(n^3). I've minimized the list comprehensions for clarity. The multiple obj['_id'] = {'$oid': str(obj['_id']}
are necessary to convert the ObjectId
to something that can be serialized to json.
@app.route('/home/')
def home():
parents = Parent.objects.all()
temps = []
for parent in parents:
p = parent.to_mongo()
# At this point, the children of parent and p are references only
p['_id'] = {'$oid': str(p['_id'])
temp_children = []
for child in parent.children:
# Now the child is dereferenced
c = child.to_mongo()
c['_id'] = {$oid': str(c['_id'])}
# Children have links back to Parent. Keep these as references.
c['parents'] = [{'oid': str(parent_ref)} for parent_ref in c['parents']]
temp_children.append(c)
p['children'] = temp_children
temps.append(parent.to_mongo())
return render_template('home.html', items=temps)
The following do not work but result in non-dereferenced children:
json.loads(json.dumps(accounts))