Saturday, 2 February 2019

Flask-Restplus how to combine data from 2 table into single data response?

I am using Flask-Restplus and SqlAlchemy to develop my API.

I have a query that looks like this:

details = db.session.query(User, Device).filter(User.id == Device.id) \
                    .filter(User.email== data['email'])\
                    .all()

For this moment,the result of the query above which I print() in console is like this:

[(<User 'None'>, <Device 1>)]

Expected Response:

{
  "data": [
    [
      {
        "id": 20,
        "name": null,
        "token": "Some String here"
      }
    ]
  ]
}

Here is my DTO:

class UserDto:
//this is for input
   user = api.model('user', {
     'email': fields.String(required=False, description='phone number'),
    'name': fields.String(required=False, description='username'),
     'device_id': fields.String(required=False,description='user_device_id'),

   })

   //this is for output
   details = api.model('details', {
    'id': fields.Integer(required=False, description='the id'),
    'name': fields.String(required=False, description='name'),
    'token': fields.String(required=False, description='token')
   })

Model for User and Device

class User(db.Model):
    __tablename__ = "users_info"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.Integer, unique=True, nullable=True)
    email = db.Column(db.String)
    device = db.relationship('Device', backref='user')
    # .. some other field here


class Device(db.Model):
    __tablename__ = "user_device"

    user_device_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey(User.id))
    token = db.Column(db.String, nullable=True)
    # .. some other field here

Controller

api = UserDto.api
_user = UserDto.user
_details = UserDto.details

@api.route('/')
class User(Resource):
    @api.response(201, 'successful')
    @api.expect(_user, validate=True)
    @api.marshal_list_with(_details, envelope='data')
    def post(self):
        data = request.json
        return user(data=data)

Actual Response:

{
  "data": [
    [
      {
        "id": 20,
        "name": null,
        "token": null
      },
      {
        "id": 20,
        "name": null,
        "token": "some string here"
      }
    ]
  ]
}

As you can see here, the same record appears 2 times (once with token being null and once with token with the string I want).

Question: How can I achieve the response that I want above?



from Flask-Restplus how to combine data from 2 table into single data response?

No comments:

Post a Comment