I want to get results from a query and send it with jsonify
to the client, I create a function that gets an instance of the model, use its 'dict' attribute delete unused values like '_sa_instance_state'
and then return new dictionary as a jsonified object to client.
Here is my code:
def display(Object):
data= Object.__dict__.copy()
del data['_sa_instance_state']
return data`
user = User.query.first()
@app.route('/')
def main():
return jsonify(display(user))
Sometimes i get this error
TypeError: Object of type Admin is not JSON serializable
where admin is
another model
that has a backref in User model.
and weird thing happens here, because sometime without changing anything I don't get the error.
When i tried to find out what's happening in a jupyter notebook, I see that __dict__
doesn't show the backrefs in jupyter notebook
but sometimes it show the backrefs when I call it from the flask app.