1

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)
techebTest
  • 67
  • 1
  • 8
  • Maybe you'll get better suggestions, but your existing code can be improved in a very simple way: The class members are a constant, you need to build it only once, not in the loop. – VPfB Oct 19 '18 at 12:27
  • Could you attach `Log` model and a few records? `db.session.add(Logs(...)), ... db.session.commit()` – Danila Ganchar Oct 19 '18 at 12:56
  • [here](http://ardupilot.org/copter/docs/common-downloading-and-analyzing-data-logs-in-mission-planner.html#message-details-copter-specific) the params look like this. And I edited my message for example log snippet. – techebTest Oct 19 '18 at 14:32

0 Answers0