I am developing my first Flask application. In this application I have a put request from which I am querying the database. However I am not able to access and display the result of the query.
I have had look at some of the similar questions but none is solving the problem. I have had a look at sqlalchemy documentation, but this is not clear to me .html#sqlalchemy.orm.Query.filter_by
this my model:
from db import db
class ConfigsModel(db.Model):
__tablename__ = "configs"
id = db.Column(db.Integer, primary_key=True)
client_id = db.Column(db.Integer, db.ForeignKey("clients.id"), unique=False, nullable=False)
endpoint = db.Column(db.String, unique=False, nullable=True)
purpose = db.Column(db.String(50), unique=False, nullable=True)
is_active = db.Column(db.Integer,unique=False, nullable=True)
credentials_data = db.Column(db.String, unique=False, nullable=True)
data = db.Column( db.Text, unique=False, nullable=True)
@classmethod
def find_by_purpose_and_id(cls, client_id, purpose):
return cls.query.filter_by(client_id= client_id,purpose=purpose)
db is an SQLAlchemy object. This is my class with the put method.
from common.schemas.ConfigSchema import ConfigSchema
from flask_smorest import Blueprint , abort
from flask.views import MethodView
from models.ConfigsModel import ConfigsModel
blp = Blueprint("configs", __name__, description="configuration Operations" )
@blp.route("/v1/config/<string:client_id>")
class ConfigController(MethodView):
@blp.arguments(ConfigSchema)
@blp.response(200, ConfigSchema)
def put(self, request_data, client_id):
configs = ConfigsModel.find_by_purpose_and_id(12, 'INITIAL')
if not configs:
abort(404, message="Missing Configuration for client")
for config in configs:
print(config.endpoint)
print(configs)
return configs
This the Schema class:
from marshmallow import Schema, fields
class ConfigSchema(Schema):
id= fields.Int(dump_only=True)
client_id = fields.Int(required=True)
endpoint = fields.Str(required=False)
purpose = fields.Str(required=False)
credentials_data = fields.Str(required=False)
How do I get the result returned by query and function find_by_purpose_and_id ?
I am developing my first Flask application. In this application I have a put request from which I am querying the database. However I am not able to access and display the result of the query.
I have had look at some of the similar questions but none is solving the problem. I have had a look at sqlalchemy documentation, but this is not clear to me https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.filter_by
this my model:
from db import db
class ConfigsModel(db.Model):
__tablename__ = "configs"
id = db.Column(db.Integer, primary_key=True)
client_id = db.Column(db.Integer, db.ForeignKey("clients.id"), unique=False, nullable=False)
endpoint = db.Column(db.String, unique=False, nullable=True)
purpose = db.Column(db.String(50), unique=False, nullable=True)
is_active = db.Column(db.Integer,unique=False, nullable=True)
credentials_data = db.Column(db.String, unique=False, nullable=True)
data = db.Column( db.Text, unique=False, nullable=True)
@classmethod
def find_by_purpose_and_id(cls, client_id, purpose):
return cls.query.filter_by(client_id= client_id,purpose=purpose)
db is an SQLAlchemy object. This is my class with the put method.
from common.schemas.ConfigSchema import ConfigSchema
from flask_smorest import Blueprint , abort
from flask.views import MethodView
from models.ConfigsModel import ConfigsModel
blp = Blueprint("configs", __name__, description="configuration Operations" )
@blp.route("/v1/config/<string:client_id>")
class ConfigController(MethodView):
@blp.arguments(ConfigSchema)
@blp.response(200, ConfigSchema)
def put(self, request_data, client_id):
configs = ConfigsModel.find_by_purpose_and_id(12, 'INITIAL')
if not configs:
abort(404, message="Missing Configuration for client")
for config in configs:
print(config.endpoint)
print(configs)
return configs
This the Schema class:
from marshmallow import Schema, fields
class ConfigSchema(Schema):
id= fields.Int(dump_only=True)
client_id = fields.Int(required=True)
endpoint = fields.Str(required=False)
purpose = fields.Str(required=False)
credentials_data = fields.Str(required=False)
How do I get the result returned by query and function find_by_purpose_and_id ?
The solution was as Detlef suggested. I just added .all() to the query.
@classmethod
def find_by_purpose_and_id(cls, client_id, purpose):
return cls.query.
filter_by(client_id= client_id,purpose=purpose).all()
.all()
to the end of your query. – Detlef Commented Jan 6 at 22:33