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

Wrong data type for cells #6

Closed askoretskiy closed 5 years ago

askoretskiy commented 5 years ago

My serializer returns type-reach content:

OrderedDict([('link', u'https://yandex.com'), ('name', u'name'), ('num', 123), ('post_date', datetime.datetime(2018, 11, 5, 17, 12, 42)), ('num2', 1.23), ('in_my_network', False)])

But in resulting XLSX all these cells are strings. E.g. '123

I didn't find in documentation any way to specify that given cell is integer / float / data / boolean.

askoretskiy commented 5 years ago

I just got stumbled on this code:

        for row in results:
            column_count = 0
            row_count += 1
            for column_name, value in row.items():
                if column_name == 'row_color':
                    continue
                column_count += 1
                cell = ws.cell(row=row_count,
                               column=column_count,
                               value='{}'.format(value)) # <----- WTF? That would convert everything to text
                cell.style = body_style
FlipperPA commented 5 years ago

IIRC, this was put in to handle dates. We are limited by the data passed from Django REST Framework. I'll dig in and see if (a) the metadata of column type is provided to the renderer by DRF, and (b) if it is worth building out a mapping.

FlipperPA commented 5 years ago

@askoretskiy After digging into the code (it had been a while!), I'm trying to recall why that was put into place. I've done some testing against my company's data endpoints to look for the exception it was handling, but they seem to be working well. I've pushed out a change as commit 497772a; any chance you can test against the latest master and see if it is working for you now?

FlipperPA commented 5 years ago

Fixed here; will be released in about 10 minutes to PyPI: https://github.com/wharton/drf-renderer-xlsx/commit/497772a9d797dc1b2e0bbbeb695278a87e7fff4a#diff-643b3dcb780e20c4ead30d169b1ff991R158