django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

UnboundLocalError: local variable 'column_names_first_row' referenced before assignment #18

Closed adiavt closed 4 years ago

adiavt commented 4 years ago

I get this error message when trying to do export to xlsx file: I'm using python 3.6.5 on windows 10 64bit

Request Method: GET

http://192.168.1.154:8005/api/transaction/excel/taxi/?start=2019-12-30&end=2019-12-30&search= 2.2.4 UnboundLocalError local variable 'column_names_first_row' referenced before assignment C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\lib\site-packages\drf_renderer_xlsx\renderers.py in render, line 122 C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\python.exe 3.6.5 ['D:\Projects\Python\nguraharaitaxi-backend', 'C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\python36.zip', 'C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36', 'C:\Users\WEBDEV-5\AppData\Local\Programs\Python\Python36\lib\site-packages'] Sen, 30 Des 2019 17:23:42 +0800

adiavt commented 4 years ago

I already figured this out, the problem is when checking for columns name first row:

if isinstance(results, ReturnDict):
    column_names_first_row = results
elif isinstance(results, ReturnList):
    column_names_first_row = self._flatten(results[0])

Checking if the results is dictionary or list did not work.

I've added this extra check:

elif type(results) is list:
    column_names_first_row = self._flatten(results[0])
elif type(results) is dict:
    column_names_first_row = results

maybe you can fix this issue and provide some more relevant data type checking, thanks.

FlipperPA commented 4 years ago

Thanks, @adippp - I've applied this fix in the 0.3.7 release which will be coming shortly.