Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
159 stars 67 forks source link

argument must be a string, a bytes-like object or a number, not 'NoneType' #127

Open evollution opened 4 years ago

evollution commented 4 years ago
@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())  

and i get error: Object of type Customer is not JSON serializable

evollution commented 4 years ago

params {'draw': '1', 'columns[0][data]': '0', 'columns[0][name]': '', 'columns[0][searchable]': 'true', 'columns[0][orderable]': 'true', 'columns[0][search][value]': '', 'columns[0][search][regex]': 'false', 'columns[1][data]': '1', 'columns[1][name]': '', 'columns[1][searchable]': 'true', 'columns[1][orderable]': 'true', 'columns[1][search][value]': '', 'columns[1][search][regex]': 'false', 'order[0][column]': '0', 'order[0][dir]': 'asc', 'start': '0', 'length': '10', 'search[value]': '', 'search[regex]': 'false', '_': '1584207848563'}

evollution commented 4 years ago

{'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}]}

This is the (rowTable.output_result() output

evollution commented 4 years ago

Another issue, because as item[0] is returned the query itself, item[-1] is missing :( @Pegase745 can you take a look why it returns item[0] as a query object instead of the first columnDT selected?

westaytroy commented 4 years ago

I had the same problem. Try using a query like in the example with select_from query = db.session.query().select_from(Customer) This worked for me. SOmehow the framework can't work with DB objects sadly.

FrankyBoy commented 3 years ago

heh, wish it was just db types ... "int() argument must be a string, a bytes-like object or a number, not 'NoneType'" ... hmmmm

westaytroy commented 3 years ago

heh, wish it was just db types ... "int() argument must be a string, a bytes-like object or a number, not 'NoneType'" ... hmmmm

This happens mostly if any of the values required are not set. I figured out that the framework assumes that at least "start" and "length" are set when init is called. my quick workaround for that: params = request.args.to_dict() if "start" not in params: params["start"] = 0 if "length" not in params: params["length"] = -1 some error handling would have been nice here.

FrankyBoy commented 3 years ago

Good to know, thanks :) Maybe this should actually be a PR.