vertliba / django-rest-framework-datatables-editor

Seamless integration between Django REST framework, Datatables and Datatables Editor.
MIT License
30 stars 15 forks source link

Rendering failure when data record has a field called "results" #8

Closed ve3jrw closed 4 years ago

ve3jrw commented 4 years ago

I have a record that has a field called "results" in it.

This results in an error in DatatablesRenderer object of type 'int' has no len().

Here is an example of the data that causes the problem going through the renderer:

GET /api/optimizers/198/

HTTP 200 OK Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS Content-Type: application/json Vary: Accept

{ "rundatetime": "2020-04-02T18:08:44.574532-04:00", "startdate": "2005-01-03", "enddate": "2020-04-01", "optrange": "(5,250) (5,250)", "results": 60025, "runtime": 2130, "optimizeresults": [ {

the problem is that this field is triggering a reference using the same name in the code (stating line 22 in renderers.py):

    if 'recordsTotal' not in data:
        # pagination was not used, let's fix the data dict
        if 'results' in data:
            results = data['results']
            try:
                **count = data['count'] if 'count' in data else len(results)**
            except:
                count = results
        else:
            results = data
            count = len(results)
        new_data['data'] = results

the logic is picking up the record "results" reference and causes the exception. Here is a local variable capture of with an assert just before line 25 when the exception occurs:

new_data {} renderer_context {'args': (), 'kwargs': {'pk': '198'}, 'request': <rest_framework.request.Request object at 0x7f31d6b09048>, 'response': <Response status_code=200, "application/json">, 'view': <portfolio.views.optimize.OptimizeViewSet object at 0x7f31d6b01f28>} request <rest_framework.request.Request object at 0x7f31d6b09048> results 60025 self <rest_framework_datatables_editor.renderers.DatatablesRenderer object at 0x7f31d6b09438> view <portfolio.views.optimize.OptimizeViewSet object at 0x7f31d6b01f28>

The results variable at this point is the record field "results" (integer value 60025) not the expected record list in the code.

There needs to be a context check that the "results" data entry retrieved is not part of the data record being transferred.

ve3jrw commented 4 years ago

Ignore this. The data structure I setup is not suitable for this usage (was trying to nest the table data and then reference the nested data in the table.)