python - jsonify a SQLAlchemy result set in Flask -


i'm trying jsonify sqlalchemy result set in flask/python.

the flask mailing list suggested following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-collection-result/#04a0754b63387f87e59dda564bde426e :

return jsonify(json_list = qryresult) 

however i'm getting following error back:

typeerror: <flaskext.sqlalchemy.basequery object @ 0x102c2df90>  not json serializable 

what overlooking here?

i have found question: how serialize sqlalchemy result json? seems similar didn't know whether flask had magic make easier mailing list post suggested.

edit: clarification, model looks like

class rating(db.model):      __tablename__ = 'rating'      id = db.column(db.integer, primary_key=true)     fullurl = db.column(db.string())     url = db.column(db.string())     comments = db.column(db.text)     overall = db.column(db.integer)     shipping = db.column(db.integer)     cost = db.column(db.integer)     honesty = db.column(db.integer)     communication = db.column(db.integer)     name = db.column(db.string())     ipaddr = db.column(db.string())     date = db.column(db.string())      def __init__(self, fullurl, url, comments, overall, shipping, cost, honesty, communication, name, ipaddr, date):         self.fullurl = fullurl         self.url = url         self.comments = comments         self.overall = overall         self.shipping = shipping         self.cost = cost         self.honesty = honesty         self.communication = communication         self.name = name         self.ipaddr = ipaddr         self.date = date 

it seems haven't executed query. try following:

return jsonify(json_list = qryresult.all()) 

[edit]: problem jsonify is, objects cannot jsonified automatically. python's datetime fails ;)

what have done, add property (like serialize) classes need serialized:

def dump_datetime(value):     """deserialize datetime object string form json processing."""     if value none:         return none     return [value.strftime("%y-%m-%d"), value.strftime("%h:%m:%s")]  class foo(db.model):     # ... sqlalchemy defs here..     def __init__(self, ...):        # self.foo = ...        pass      @property     def serialize(self):        """return object data in serializeable format"""        return {            'id'         : self.id,            'modified_at': dump_datetime(self.modified_at),            # example how deal many2many relations            'many2many'  : self.serialize_many2many        }     @property     def serialize_many2many(self):        """        return object's relations in serializeable format.        nb! calls many2many's serialize property.        """        return [ item.serialize item in self.many2many] 

and views can do:

return jsonify(json_list=[i.serialize in qryresult.all()]) 

hope helps ;)


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -