sqlalchemy-datatables TypeError:类型为Customer的对象不可JSON序列化 [英] sqlalchemy-datatables TypeError: Object of type Customer is not JSON serializable

查看:59
本文介绍了sqlalchemy-datatables TypeError:类型为Customer的对象不可JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SQLAlchemy-DataTables 构建具有服务器端处理功能的表,

I am trying to use SQLAlchemy-DataTables to build a table with server-side processing,

   @app.route("/data", methods=['GET'])
        def data():
        columns = [
            ColumnDT(Customer.id),
            ColumnDT(Customer.Email),
        ]
        query = db.session.query(Customer)
        params = request.args.to_dict()
    
        
        rowTable = DataTables(params, query, columns)
        print(query , file=sys.stdout)
        return jsonify(rowTable.output_result()) 

在调试模式下运行时,我可以看到rowTable.output_result()返回:

while running in debug mode I can see that rowTable.output_result() returns:

{'draw': '1', 'recordsTotal': '13997', 'recordsFiltered': '13997', 'data': [{'0': <Customer#1>, '1': 1}, {'0': <Customer#2>, '1': 2}, {'0': <Customer#3>, '1': 3}, {'0': <Customer#4>, '1': 4}, {'0': <Customer#5>, '1': 5}, {'0': <Customer#6>, '1': 6}, {'0': <Customer#7>, '1': 7}, {'0': <Customer#8>, '1': 8}, {'0': <Customer#9>, '1': 9}, {'0': <Customer#10>, '1': 10}]}

但是出现以下错误:TypeError:类型为Customer的对象不可JSON序列化

but i get the following error: TypeError: Object of type Customer is not JSON serializable

我按照文档进行了所有操作,所以我不知道为什么它不起作用

I did everything as per documentation so I can't figure out why this is not working

推荐答案

问题是您试图在python对象< Customer> jsonify()>.要了解为什么会这样,请查看您的输出:

The problem is that you are trying to call jsonify() on the python object <Customer>. To see why this is, look at your output:

{
  'draw': '1',
  'recordsTotal': '13997',
  'recordsFiltered': '13997',
  'data': [{
    '0': < Customer1 > ,
    '1': 1
  }, {
    '0': < Customer2 > ,
    '1': 2
  }, {
    '0': < Customer3 > ,
    '1': 3
  }, {
    '0': < Customer4 > ,
    '1': 4
  }, {
    '0': < Customer5 > ,
    '1': 5
  }, {
    '0': < Customer6 > ,
    '1': 6
  }, {
    '0': < Customer7 > ,
    '1': 7
  }, {
    '0': < Customer8 > ,
    '1': 8
  }, {
    '0': < Customer9 > ,
    '1': 9
  }, {
    '0': < Customer10 > ,
    '1': 10
  }]
}

'data'内部,您无法通过 json.dumps()自动序列化的对象,而 jsonify()正是该对象为你.

Inside 'data' you have objects that cannot be automatically serialized by json.dumps() which is what jsonify() effectively does for you.

要解决此问题,您需要修改输出,以使 json.dumps()具有更多无聊的数据类型.例如:

To fix this problem, you need to modify the output so that the json.dumps() has more boring data types to work with. For example:

for item in rowTable.output_result()['data']:
    item['0'] = {
        'customer_id': item['0'].id,
        # and so on for the other properties you're interested in
    }

您还可以在< Customer> 类上编写一个函数,该函数会自动序列化所有重要属性,然后您可以执行以下操作:

You could also write a function on the <Customer> class that automatically serializes all the important properties, and then you could just do something like:

for item in rowTable.output_result()['data']:
    item['0'] = item['0'].serialized

以及如何在课堂上做到这一点的示例:

and the example for how to do that on the class:

class Customer(Model):
    id = Column(Integer)
    # etc...

    @property
    def serialized(self):
        return {
            'id': self.id
        }


这篇关于sqlalchemy-datatables TypeError:类型为Customer的对象不可JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆