[FIXED] Flask-restful How to `marshal_with()` data in reference_field of mongoengine?


I use Flask-restful and MongoEngine,I want to show the data in ReferenceField of my model in the JSON.

I have this 2 model, RoomModel have a ReferenceField to UserModel:

class RoomModel(Document):
    meta = {'allow_inheritance': True}

    room_name = StringField()
    created_by = ReferenceField(UserModel)

class UserModel(Document):

    username = StringField()
    email = StringField()

I want to marshal_with() all the data get from the mongoengine query to the response,so I done this:

     room_model_fields = {
        'room_name': fields.String,
        'created_by': fields.String(attribute=UserModel._id)

    room_model_fields_format = {
        'rooms': fields.List(fields.Nested(room_model_fields))

    def get():
        room = RoomModel.objects().all() 
        return {'rooms': room},200

I get this response:

 rooms": [
      "room_name": "mobile_legend",
      "created_by": "UserModel object" <<--Now: This only return a string with "UserModel object"
      "created_by": "SOME_ID_STRING"  <<-- THIS IS WHAT I WANT

I see my database,it have a field with created_by: SOME_ID_STRING_HERE,I want the SOME_ID_STRING show in the response instead of “UserModel object” string.

How can I solve this problem?


You need to use attribute='created_by.id'. See the example below:

from mongoengine import StringField
from flask_restful import fields, marshal_with

class Car(Document):
    brand = StringField()

class User(Document):
    car = ReferenceField(Car)
    name = StringField()

car = Car(brand='VW').save()
user = User(car=car, name='John Doe').save()

@marshal_with({'name': fields.String, 'id': fields.String, 'car_id': fields.String(attribute='car.id')})
def get():
    return user

print(dict(get()))    # {'car_id': u'5e124e83aa7a179157418ab2', 'name': u'John Doe', 'id': u'5e124e83aa7a179157418ab3'}

Answered By – bagerard

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published