0
我想返回查询的结果为JSON。我正在使用以下路线将一个模型实例作为JSON对象返回。如何以JSON的形式返回结果列表?
@mod.route('/autocomplete/<term>', methods=['GET'])
def autocomplete(term):
country = Country.query.filter(Country.name_pt.ilike('%'+ term + '%')).first()
country_dict = country.__dict__
country_dict.pop('_sa_instance_state', None)
return jsonify(json_list=country_dict)
此代码工作得很好,如果我使用first()
方法。但是,我需要使用all()
来获取所有结果。当我这样做时,我得到以下错误。
country_dict = country.__dict__
AttributeError: 'list' object has no attribute '__dict__'
我应该怎么做才能将整个结果列表作为JSON返回?