sqlalchemy-datatables icon indicating copy to clipboard operation
sqlalchemy-datatables copied to clipboard

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

Open evollution opened this issue 4 years ago • 7 comments

@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 avatar Mar 14 '20 14:03 evollution

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 avatar Mar 14 '20 17:03 evollution

{'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 avatar Mar 14 '20 17:03 evollution

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?

evollution avatar Mar 15 '20 13:03 evollution

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.

westaytroy avatar Mar 20 '20 08:03 westaytroy

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

FrankyBoy avatar Jul 29 '21 15:07 FrankyBoy

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.

westaytroy avatar Jul 30 '21 08:07 westaytroy

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

FrankyBoy avatar Jul 30 '21 17:07 FrankyBoy