mjumbewu / django-rest-framework-csv

CSV Tools for Django REST Framework
BSD 2-Clause "Simplified" License
364 stars 89 forks source link

tablize(data) doesn't return headers if data is empty #32

Closed dbkaplun closed 8 years ago

munendrasn commented 9 years ago

@dbkaplun in renderers.py

# in tablize function
if data : # data is empty , this becomes false 
     # do something
else:   # executed if data is empty
    return []

hence, when data is empty, no headers returned If you remove above if else the code will be like this

def tablize(self, data):
            data = self.flatten_data(data) 
            # for empty data, headers will be None
            data.header = data.header or self.headers

            if not data.header:
                headers = set()
                for item in data:  # for empty data , wont enter loop this is the reason for not returning headers
                    headers.update(list(item.keys()))
                data.header = sorted(headers)
            rows = []
            for item in data:  # for empty data , wont enter loop
                row = []
                for key in data.header:
                    row.append(item.get(key, None))
                rows.append(row)

            return [data.header] + rows # empty list is returned

If you want headers to be returned, refer to #27 for more details. hope this answers your question