title: MongoEngine - How to deference a List Field efficiently when converting to json
body: <pre><code>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)
  </code></pre>
  
  <p>I have two collections similar to that above, which maintain a many to many relationship.</p>
  
  <p>In the template with Angular, I'm setting a javascript variable to a list of Parents like so:</p>
  
  <pre><code>$scope.items = {{ parents|tojson }};
  </code></pre>
  
  <p>This results in an array of Parents who'se <code>chilren</code> are an array of Object Ids (references), as opposed to dereferenced <code>child</code> objects:</p>
  
  <pre><code>$scope.items = [{'$oid': '123', 'name': 'foo', 'children': [{'$oid': '456'}]}];
  </code></pre>
  
  <p>I want this angular object to contain all of the dereferenced children.  Is there an efficient way to do this?</p>
  
  <p>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 <code>obj['_id'] = {'$oid': str(obj['_id']}</code> are necessary to convert the <code>ObjectId</code> to something that can be serialized to json.</p>
  
  <pre><code>@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)            
  </code></pre>
  
  <p>The following do not work but result in non-dereferenced children:</p>
  
  <pre><code>json.loads(json.dumps(accounts))
  </code></pre>