Nesting output of 2 Schemas into one Flask Marshmallow

Viewed 120

Here I have the following two Schemas:

class ProviderLocationIdSchema(Schema):
    id = fields.Int(attribute='location_id', dump_only=True, many=True)

class ProviderUserSchema(Schema):
    id = fields.Int()
    user_id = fields.Int()
    first_name = fields.Str()
    last_name = fields.Str()
    email = fields.Str()
    contact_number = fields.Str()
    provider_id = fields.Int()
    user_type = UserTypeField(attribute='user_type_id')

I would as an end result like to nest the providerLocationIdSchema inside the providerUserSchema as a location_ids list. instead of having two separate outputs, but am not sure how to achieve this result with the current way the DB is being queried. Any help would be quite appreciated.

here is my supporting code:

@bp.route("/providers/<int:provider_id>/users", methods=["GET"])
def list_provider_users(provider_id):

    user_list = db_session.query(User.first_name, User.last_name, User.email,
                            User.contact_number, ProviderUser.id, ProviderUser.user_id,
                            ProviderUser.provider_id, ProviderUser.user_type_id) \
            .select_from(User).join(ProviderUser)\
            .filter(ProviderUser.provider_id == provider_id)\
            .all()

    location_ids = db_session.query(ProviderUserLocationLink.location_id) \
            .filter(ProviderUserLocationLink.provider_user_id == provider_id)\
            .all()

    locations_response = location_ids_schema.dump(location_ids) 
    users_response = provider_users_schema.dump(user_list)

    return jsonify(# ????)
0 Answers
Related