title: How to get ReferenceField data in mongoengine?
body: <p>I'm have a problem that query set retrieving <code>oid</code> in json, and I would like to retrieve actual username of that User collection I have below:</p>
  
  <pre><code>class User(db.Document):
      username      = db.StringField(required=True)
      password_hash = db.StringField()
      is_admin      = db.IntField(default=0)
  
  class Message(db.EmbeddedDocument):
      to_users     = db.ListField(db.ReferenceField(User))
      created_at   = db.DateTimeField(default=datetime.now)
      is_read      = db.BooleanField(default=False)
      body         = db.StringField(required=True)
  
  class Inbox(db.Document):
      from_user    = db.ReferenceField(User, required=True)
      subject      = db.StringField(max_length=255, required=True)
      created_at   = db.DateTimeField(default=datetime.now)
      messages     = db.ListField(db.EmbeddedDocumentField(Message))
  
  username = User().get_username()
  username = User.objects(username=username).first()
  
  inbox = Inbox.objects.filter(messages__to_users__in=[username]).only('from_user', 'subject', 'created_at').to_json()
  </code></pre>
  
  <p>And that's the result which I get:</p>
  
  <pre><code>[{"created_at": {"$date": 1401593024844}, "from_user": {"$oid": "538ad45fb43fdd69076d3e64"}, "subject": "test"}]
  </code></pre>
  
  <p>Would be nice to have something like this:</p>
  
  <pre><code>[{"created_at": {"$date": 1401593024844}, "from_user": {"username": "holms"}, "subject": "test"}]
  </code></pre>
  
  <p>or somehow similar like <code>"from_user" : {"User": {"username":"holms"}}</code></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Above answer works for the ReferenceField that is in Document, but what about EmbeddedDocument references? Let's say I'll add "messages" to only()?</p>
  
  <pre><code>In [97]: inbox = Inbox.objects(messages__to_users__in=[username]).only('from_user', 'subject', 'created_at', 'messages').select_related()
  </code></pre>
  
  <p>Currently I've found the way by mapping dictionary which is quite painful.</p>
  
  <pre><code>In [125]: for entry in inbox:
      item = dict(subject=entry.subject, messages=map(lambda msg: dict(body=msg.body, is_read=msg.is_read, to_users=map(lambda usr: usr.username, msg.to_users)), entry.messages), from_user=entry.from_user.username)
      result.append(item)
     .....:
  
  In [126]: result
  Out[126]:
  [
   {'from_user': u'holms',
    'messages': [{'body': u'test body',
      'is_read': False,
      'to_users': [u'holms']}],
    'subject': u'test'}]
  </code></pre>
  
  <p><strong>UPDATE 2</strong> Seems to be this done the trick</p>
  
  <pre><code> 57     def to_json(self):
   58         data = self.to_mongo() # get pymongo representation
   59         data["from_user"] = {"User": {"username": self.from_user.username} }
   60
   61         for key, reply in enumerate(data["messages"]):
   62             for user in self.messages[key].to_users:
   63                 print user.username
   64             reply["to_users"] = {"User": {"username": user.username} }
   65
   66         return json_util.dumps(data)
  </code></pre>
  
  <p>and you get this :)</p>
  
  <pre><code>    {
         "_id":{
            "$oid":"538ad500b43fdd690e2eefb5"
         },
         "from_user":{
            "User":{
               "username":"holms"
            }
         },
         "subject":"test",
         "created_at":{
            "$date":1401593024844
         },
         "messages":[
            {
               "to_users":{
                  "User":{
                     "username":"holms"
                  }
               },
               "created_at":{
                  "$date":1401593024845
               },
               "is_read":false,
               "body":"test body"
            }
         ]
  
      }
  </code></pre>