代码之家  ›  专栏  ›  技术社区  ›  Eddie Parker

cherrypy+sqlAlchemy+sqlAlchemy数据表:不可显示类型:dict

  •  0
  • Eddie Parker  · 技术社区  · 6 年前

    我正在尝试将jquery数据表服务器端处理与cherrypy&sqlAlchemy集成在一起,并得到一个关于“unhashashable type:'dict'”的错误。

    我写的代码如下:

    @cherrypy.tools.json_out()
    def GET(self, *args, **kwargs):
        with GetDb().ScopedSession("Fetching detail for main page") as session:
            # defining columns
            columns = [
                ColumnDT(dbModels.DatabaseModel.Field1),
                ColumnDT(dbModels.DatabaseModel.Field2),
                # ...
            ]
    
            query = session.query(dbModels.DatabaseModel)
    
            # GET parameters
            params = kwargs  # I think this is what's causing the issue.
    
            print("==========================")
            print(params)
            print("==========================")
    
            # instantiating a DataTable for the query and table needed
            rowTable = DataTables(params, query, columns)
    
            # returns what is needed by DataTable
            result = rowTable.output_result()
            print("==========================")
            print(result)
            print("==========================")
            return result
    

    “params”打印的结果如下(稍微被截断):

    {'draw': '1', 'columns[0][data]': '', 'columns[0][name]': '', 'columns[0][searchable]': 'true' } 
    

    结果输出如下:

    {'draw': '1', 'recordsTotal': '125', 'recordsFiltered': '125', 'error': "unhashable type: 'dict'"}
    

    我的怀疑是dataTables()调用不喜欢“jsonPath”这样的get参数(参见上面的“我认为这是导致问题的原因”部分),所以我很好奇是否有一种方法可以让cherrypy将**kwargs作为嵌套的键/值对返回,或者让函数将kwargs转换成嵌套的字典——或者,如果我完全错误,并且没有他的错误完全不同。:)

    谢谢!

    更多数据,如果有帮助:

    网页呈现会显示一条警告:

    DataTables warning: table id=active-jobs - unhashable type: 'dict'
    

    单击“确定”会在数据表代码中产生类型错误:

    datatables.min.js:52 Uncaught TypeError: Cannot read property 'length' of undefined
        at vb (datatables.min.js:52)
        at datatables.min.js:49
        at i (datatables.min.js:47)
        at Object.success (datatables.min.js:48)
        at fire (jquery-3.3.1.js:3268)
        at Object.fireWith [as resolveWith] (jquery-3.3.1.js:3398)
        at done (jquery-3.3.1.js:9305)
        at XMLHttpRequest.<anonymous> (jquery-3.3.1.js:9548)
    vb @ datatables.min.js:52
    (anonymous) @ datatables.min.js:49
    i @ datatables.min.js:47
    success @ datatables.min.js:48
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    done @ jquery-3.3.1.js:9305
    (anonymous) @ jquery-3.3.1.js:9548
    load (async)
    send @ jquery-3.3.1.js:9567
    ajax @ jquery-3.3.1.js:9206
    sa @ datatables.min.js:48
    lb @ datatables.min.js:49
    P @ datatables.min.js:41
    T @ datatables.min.js:43
    ha @ datatables.min.js:60
    e @ datatables.min.js:105
    (anonymous) @ datatables.min.js:105
    each @ jquery-3.3.1.js:354
    each @ jquery-3.3.1.js:189
    n @ datatables.min.js:95
    h.fn.DataTable @ datatables.min.js:177
    RebuildTable @ (index):128
    (anonymous) @ (index):266
    mightThrow @ jquery-3.3.1.js:3534
    process @ jquery-3.3.1.js:3602
    setTimeout (async)
    (anonymous) @ jquery-3.3.1.js:3640
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    fire @ jquery-3.3.1.js:3406
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    ready @ jquery-3.3.1.js:3878
    completed @ jquery-3.3.1.js:3888
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Eddie Parker    6 年前

    我终于明白了。我的一列是来自sqlachemy_utils的“json type”类型,sqlachemy datatables类没有处理它正在为json数据返回“dict”的问题。

    我解决了这个问题——不管是好是坏——不把它包括在我的列中,而是有一个手工制作的列,用我需要的细节填充一个对象。