How to jsonify objects from sqlalchemy?

后端 未结 6 556
离开以前
离开以前 2021-01-21 23:15

I\'m using Flask, SQLAlchemy and javascript. I need to pass the results of my query to javascript in the json format, through AJAX, but I keep getting this error:



        
6条回答
  •  盖世英雄少女心
    2021-01-22 00:10

    Like the error message says, you cannot call jsonify on a SQLAlchemy model object (I don't think you can pickle them either). Try:

    clouds_d[idx] = dict((k, cloud.__dict__[k]) for k in cloud.__dict__ if k[0] != '_')
    

    or better yet, be explicit:

    clouds_d[idx] = {
        'id': cloud.id,
        'other_attrs': '...',
    }
    

提交回复
热议问题