Home > Enterprise >  Can't make a request using an 'user_id' as parameter on a Flask ApiRest
Can't make a request using an 'user_id' as parameter on a Flask ApiRest

Time:10-05

I made this request getting all the data from all the users, and it's working just fine:

    @app.route('/account/bank-statement', methods=['GET'])
def get_bank_statement():
    try:
        bank_statement = TransferModel.query.all()
        transfer_schema = TransferSchema(many=True)
        bank_statement = transfer_schema.dump(bank_statement)
        array = np.asarray(bank_statement)
        list = array.tolist()
        return make_response(json.dumps(list), 200)
    except ValidationError as error:
        return make_response(error.messages, 422)

But when i try to get the same data from a specific user_id it returns an empty json:

@app.route('/account/bank-statement/<user_id>', methods=['GET'])
def get_bank_statement_id(user_id):
    try:
        bank_statement = TransferModel.query.get(user_id)
        transfer_schema = TransferSchema(many=True)
        bank_statement = transfer_schema.dump(bank_statement)
        array_result = np.asarray(bank_statement)
        list = array_result.tolist()
        return make_response(json.dumps(list), 200)
    except ValidationError as error:
        return make_response(error.messages, 422)

What i'm doing wrong?

this is the model for that:

class TransferModel(db.Model):
    __tablename__ = 'transfers'
    transfer_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    user_id = db.Column(db.String)
    friend_id = db.Column(db.String)
    value = db.Column(db.Integer)
    card_id = db.Column(db.String)
    date = db.Column(db.String(10))

    def __init__(self, user_id, friend_id, value, card_id, date):
        self.user_id = user_id
        self.friend_id = friend_id
        self.value = value
        self.card_id = card_id
        self.date = date

    def __repr__(self, ):
        return '' % self.user_id

    def remodel(friend_data):
        friend_data['card_id'] = friend_data['billing_card']['card_id']
        friend_data['value'] = friend_data['total_to_transfer']
        friend_data['date'] = date.today().strftime("%d/%m/%Y")
        del friend_data['billing_card']
        del friend_data['total_to_transfer']
        return friend_data

    def save_to_db(self, ):
        db.session.add(self)
        db.session.commit()

CodePudding user response:

Maybe I didn't understand properly, but shouldn't you access specific user_id rows using filter_by?

So instead of doing:

bank_statement = TransferModel.query.get(user_id)

you do:

bank_statement = TransferModel.query.filter_by(user_id=user_id).all()
  • Related