I have a flask-sqlalchemy database class which has more than 250 member variables. I need to jsonify them to response for POST request.
I found this answer here to access this members with loop and I change it like this:
Logs = apps.models.Logs.query.filter_by(FlightID=fligtID).all()
logList = []
for Log in Logs:
logDict = {}
classMembers = [attr for attr in dir(Log) if not callable(getattr(Log, attr)) and not attr.startswith("__") and not attr.startswith("_") and not attr.startswith("meta") and not attr.startswith("query")]
for member in classMembers:
logDict[member] = getattr(Log, member)
logList.append(logDict)
return jsonify(logList)
This solution works but is this the best option to make it work this when we think about performance for server side. Is there any other way to do this with flask-sqlalchemy?
Here example log snippet I am sorry to say that can't share more than this.:
class Logs(db_logs.Model):
__bind_key__ = 'logs'
__tablename__ = 'logs'
ID = db_logs.Column(db_logs.Integer, primary_key=True)
FlightID = db_logs.Column(db_logs.Integer)
foo_time = db_logs.Column('foo.time', db_logs.Float)
foo_x = db_logs.Column('foo.x', db_logs.Float)
foo_gain = db_logs.Column('foo.gain', db_logs.String(255))
bar_time = db_logs.Column('bar.time', db_logs.Float)